Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

Commit

Permalink
Apple: Shadowsocks tunnel (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
alalamav committed Sep 26, 2019
1 parent b5f5801 commit bd58355
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
15 changes: 8 additions & 7 deletions outline/android/tun2socks.go
Expand Up @@ -15,7 +15,8 @@
package tun2socks

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

"github.com/Jigsaw-Code/outline-go-tun2socks/tunnel"
Expand All @@ -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 {
Expand Down
22 changes: 14 additions & 8 deletions outline/apple/tun2socks.go
Expand Up @@ -16,7 +16,9 @@ package tun2socks

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

Expand Down Expand Up @@ -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.