Skip to content
Permalink
Browse files

Apple: Shadowsocks tunnel (#27)

  • Loading branch information...
alalamav committed Sep 26, 2019
1 parent b5f5801 commit bd58355001a866fea2c41b6c2c03457a01e27b04
Showing with 22 additions and 15 deletions.
  1. +8 −7 outline/android/tun2socks.go
  2. +14 −8 outline/apple/tun2socks.go
@@ -15,7 +15,8 @@
package tun2socks

import (
"errors"
"fmt"
"math"
"runtime/debug"

"github.com/Jigsaw-Code/outline-go-tun2socks/tunnel"
@@ -34,21 +35,21 @@ type OutlineTunnel interface {
}

// ConnectShadowsocksTunnel reads packets from a TUN device and routes it to a Shadowsocks proxy server.
// Returns an AndroidTunnel instance and does *not* take ownership of the TUN file descriptor; the
// caller is responsible for closing after AndroidTunnel disconnects.
// Returns an OutlineTunnel instance and does *not* take ownership of the TUN file descriptor; the
// caller is responsible for closing after OutlineTunnel disconnects.
//
// `fd` is the file descriptor to the VPN TUN device. Must be set to blocking mode.
// `host` is IP address of the SOCKS proxy server.
// `port` is the port of the SOCKS proxy server.
// `host` is IP address of the Shadowsocks proxy server.
// `port` is the port of the Shadowsocks proxy server.
// `password` is the password of the Shadowsocks proxy.
// `cipher` is the encryption cipher the Shadowsocks proxy.
// `isUDPEnabled` indicates whether the tunnel and/or network enable UDP proxying.
//
// Throws an exception if the TUN file descriptor cannot be opened, or if the tunnel fails to
// connect.
func ConnectShadowsocksTunnel(fd int, host string, port int, password, cipher string, isUDPEnabled bool) (OutlineTunnel, error) {
if port <= 0 || port > 65535 {
return nil, errors.New("Must provide a valid port number")
if port <= 0 || port > math.MaxUint16 {
return nil, fmt.Errorf("Invalid port number: %v", port)
}
tun, err := tunnel.MakeTunFile(fd)
if err != nil {
@@ -16,7 +16,9 @@ package tun2socks

import (
"errors"
"fmt"
"io"
"math"
"runtime/debug"
"time"

@@ -47,18 +49,22 @@ func init() {
}()
}

// ConnectSocksTunnel reads packets from a TUN device and routes it to a SOCKS server. Returns an
// OutlineTunnel instance that should be used to input packets to the tunnel.
// ConnectShadowsocksTunnel reads packets from a TUN device and routes it to a Shadowsocks proxy server.
// Returns an OutlineTunnel instance that should be used to input packets to the tunnel.
//
// `tunWriter` is used to output packets to the TUN (VPN).
// `host` is the IP address of the SOCKS proxy server.
// `port` is the port of the SOCKS proxy server.
// `host` is IP address of the Shadowsocks proxy server.
// `port` is the port of the Shadowsocks proxy server.
// `password` is the password of the Shadowsocks proxy.
// `cipher` is the encryption cipher the Shadowsocks proxy.
// `isUDPEnabled` indicates whether the tunnel and/or network enable UDP proxying.
//
// Sets an error if the tunnel fails to connect.
func ConnectSocksTunnel(tunWriter TunWriter, host string, port int, isUDPEnabled bool) (OutlineTunnel, error) {
if tunWriter == nil || host == "" || port <= 0 || port > 65535 {
return nil, errors.New("Must provide a TunWriter, a valid SOCKS proxy host and port")
func ConnectShadowsocksTunnel(tunWriter TunWriter, host string, port int, password, cipher string, isUDPEnabled bool) (OutlineTunnel, error) {
if tunWriter == nil {
return nil, errors.New("Must provide a TunWriter")
} else if port <= 0 || port > math.MaxUint16 {
return nil, fmt.Errorf("Invalid port number: %v", port)
}
return tunnel.NewOutlineTunnel(host, uint16(port), isUDPEnabled, tunWriter)
return tunnel.NewOutlineTunnel(host, port, password, cipher, isUDPEnabled, tunWriter)
}

0 comments on commit bd58355

Please sign in to comment.
You can’t perform that action at this time.