Skip to content

Commit

Permalink
refactor proxy/internal
Browse files Browse the repository at this point in the history
update proxies

refactor

fix sth

up
  • Loading branch information
xjasonlyu committed Aug 28, 2024
1 parent 8ef439b commit a9f56af
Show file tree
Hide file tree
Showing 15 changed files with 136 additions and 51 deletions.
2 changes: 1 addition & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func netstack(k *Key) (err error) {
}
}()

if _defaultProxy, err = parseProxy(k.Proxy); err != nil {
if _defaultProxy, err = proxy.ParseFromURL(k.Proxy); err != nil {
return
}
proxy.DefaultProxy = _defaultProxy
Expand Down
5 changes: 0 additions & 5 deletions engine/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/xjasonlyu/tun2socks/v2/core/device"
"github.com/xjasonlyu/tun2socks/v2/core/device/fdbased"
"github.com/xjasonlyu/tun2socks/v2/core/device/tun"
"github.com/xjasonlyu/tun2socks/v2/proxy"
)

func parseRestAPI(s string) (*url.URL, error) {
Expand Down Expand Up @@ -65,10 +64,6 @@ func parseFD(u *url.URL, mtu uint32) (device.Device, error) {
return fdbased.Open(u.Host, mtu, 0)
}

func parseProxy(s string) (proxy.Proxy, error) {
return proxy.ParseFromURL(s)
}

func parseMulticastGroups(s string) (multicastGroups []net.IP, _ error) {
ipStrings := strings.Split(s, ",")
for _, ipString := range ipStrings {
Expand Down
9 changes: 5 additions & 4 deletions proxy/direct/direct.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
M "github.com/xjasonlyu/tun2socks/v2/metadata"
"github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal/base"
)

var _ proxy.Proxy = (*Direct)(nil)

const Protocol = "direct"
const protocol = "direct"

type Direct struct{ *internal.Base }
type Direct struct{ *base.Base }

func New() *Direct { return &Direct{internal.New(Protocol, "")} }
func New() *Direct { return &Direct{base.New("", protocol)} }

func Parse(*url.URL) (proxy.Proxy, error) { return New(), nil }

Expand Down Expand Up @@ -55,5 +56,5 @@ func (pc *directPacketConn) WriteTo(b []byte, addr net.Addr) (int, error) {
}

func init() {
proxy.RegisterProtocol(Protocol, Parse)
proxy.RegisterProtocol(protocol, Parse)
}
9 changes: 5 additions & 4 deletions proxy/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,23 @@ import (
M "github.com/xjasonlyu/tun2socks/v2/metadata"
"github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal/base"
)

var _ proxy.Proxy = (*HTTP)(nil)

const Protocol = "http"
const protocol = "http"

type HTTP struct {
*internal.Base
*base.Base

user string
pass string
}

func New(addr, user, pass string) (*HTTP, error) {
return &HTTP{
Base: internal.New(Protocol, addr),
Base: base.New(addr, protocol),
user: user,
pass: pass,
}, nil
Expand Down Expand Up @@ -108,5 +109,5 @@ func basicAuth(username, password string) string {
}

func init() {
proxy.RegisterProtocol(Protocol, Parse)
proxy.RegisterProtocol(protocol, Parse)
}
8 changes: 4 additions & 4 deletions proxy/internal/base.go → proxy/internal/base/base.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package internal
package base

import (
"context"
Expand All @@ -13,13 +13,13 @@ import (
var _ proxy.Proxy = (*Base)(nil)

type Base struct {
protocol, address string
address, protocol string
}

func New(protocol, address string) *Base {
func New(address, protocol string) *Base {
return &Base{
protocol: protocol,
address: address,
protocol: protocol,
}
}

Expand Down
9 changes: 4 additions & 5 deletions proxy/internal/util.go → proxy/internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,24 @@ import (
"github.com/xjasonlyu/tun2socks/v2/transport/socks5"
)

const (
tcpKeepAlivePeriod = 30 * time.Second
)
const tcpKeepAlivePeriod = 30 * time.Second

// SetKeepAlive sets tcp keepalive option for tcp connection.
// SetKeepAlive sets the tcp keepalive option for the tcp connection.
func SetKeepAlive(c net.Conn) {
if tcp, ok := c.(*net.TCPConn); ok {
tcp.SetKeepAlive(true)
tcp.SetKeepAlivePeriod(tcpKeepAlivePeriod)
}
}

// SafeConnClose closes tcp connection safely.
// SafeConnClose closes the given tcp connection safely.
func SafeConnClose(c net.Conn, err error) {
if c != nil && err != nil {
c.Close()
}
}

// SerializeSocksAddr serializes *metadata.Metadata to socks5.Addr.
func SerializeSocksAddr(m *M.Metadata) socks5.Addr {
return socks5.SerializeAddr("", m.DstIP, m.DstPort)
}
14 changes: 7 additions & 7 deletions proxy/protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proxy

import (
"errors"
"fmt"
"net/url"
"sync"

Expand All @@ -11,7 +12,7 @@ import (
// ErrProtocol indicates that parsing encountered an unknown protocol.
var ErrProtocol = errors.New("proxy: unknown protocol")

// A proxy holds a proxy's protocol and how to parse it.
// A protocol holds a proxy protocol's name and how to parse it.
type protocol struct {
name string
parse func(*url.URL) (Proxy, error)
Expand Down Expand Up @@ -44,10 +45,9 @@ func pick(name string) protocol {
return protocol{}
}

// Parse parses a proxy url that has been encoded in a registered format.
// The string returned is the format name used during format registration.
// Format registration is typically done by an init function in the codec-
// specific package.
// Parse parses proxy *url.URL that holds the proxy info into Proxy.
// Protocol registration is typically done by an init function in the
// proxy-specific package.
func Parse(proxyURL *url.URL) (Proxy, error) {
if proxyURL == nil {
return nil, errors.New("proxy: nil url")
Expand All @@ -57,12 +57,12 @@ func Parse(proxyURL *url.URL) (Proxy, error) {
}
p := pick(proxyURL.Scheme)
if p.parse == nil {
return nil, ErrProtocol
return nil, fmt.Errorf("%w: %s", ErrProtocol, proxyURL.Scheme)
}
return p.parse(proxyURL)
}

// ParseFromURL parses a
// ParseFromURL parses url string that holds the proxy info into Proxy.
func ParseFromURL(proxy string) (Proxy, error) {
proxyURL, err := url.Parse(proxy)
if err != nil {
Expand Down
9 changes: 9 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,19 @@ const (
var DefaultProxy Proxy = nil

type Proxy interface {
// Address returns the address of the proxy.
Address() string

// Protocol returns the protocol of the proxy.
Protocol() string

// String returns the string representation of the proxy.
String() string

// DialContext is used to dial TCP networks with context.
DialContext(context.Context, *M.Metadata) (net.Conn, error)

// DialUDP is used to to dial/listen UDP networks.
DialUDP(*M.Metadata) (net.PacketConn, error)
}

Expand Down
10 changes: 5 additions & 5 deletions proxy/reject/reject.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ import (

M "github.com/xjasonlyu/tun2socks/v2/metadata"
"github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal/base"
)

var _ proxy.Proxy = (*Reject)(nil)

const Protocol = "reject"
const protocol = "reject"

type Reject struct{ *internal.Base }
type Reject struct{ *base.Base }

func New() *Reject { return &Reject{internal.New(Protocol, "")} }
func New() *Reject { return &Reject{base.New("", protocol)} }

func Parse(*url.URL) (proxy.Proxy, error) { return New(), nil }

Expand Down Expand Up @@ -48,5 +48,5 @@ func (npc *nopPacketConn) SetReadDeadline(time.Time) error { ret
func (npc *nopPacketConn) SetWriteDeadline(time.Time) error { return nil }

func init() {
proxy.RegisterProtocol(Protocol, Parse)
proxy.RegisterProtocol(protocol, Parse)
}
9 changes: 5 additions & 4 deletions proxy/relay/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ import (
M "github.com/xjasonlyu/tun2socks/v2/metadata"
"github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal/base"
)

var _ proxy.Proxy = (*Relay)(nil)

const Protocol = "relay"
const protocol = "relay"

type Relay struct {
*internal.Base
*base.Base

user string
pass string
Expand All @@ -37,7 +38,7 @@ type Relay struct {

func New(addr, user, pass string, noDelay bool) (*Relay, error) {
return &Relay{
Base: internal.New(Protocol, addr),
Base: base.New(addr, protocol),
user: user,
pass: pass,
noDelay: noDelay,
Expand Down Expand Up @@ -268,5 +269,5 @@ func serializeRelayAddr(m *M.Metadata) *relay.AddrFeature {
}

func init() {
proxy.RegisterProtocol(Protocol, Parse)
proxy.RegisterProtocol(protocol, Parse)
}
9 changes: 5 additions & 4 deletions proxy/shadowsocks/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ import (
M "github.com/xjasonlyu/tun2socks/v2/metadata"
"github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal/base"
"github.com/xjasonlyu/tun2socks/v2/transport/shadowsocks/core"
obfs "github.com/xjasonlyu/tun2socks/v2/transport/simple-obfs"
"github.com/xjasonlyu/tun2socks/v2/transport/socks5"
)

var _ proxy.Proxy = (*Shadowsocks)(nil)

const Protocol = "ss"
const protocol = "ss"

type Shadowsocks struct {
*internal.Base
*base.Base

cipher core.Cipher

Expand All @@ -38,7 +39,7 @@ func New(addr, method, password, obfsMode, obfsHost string) (*Shadowsocks, error
}

return &Shadowsocks{
Base: internal.New(Protocol, addr),
Base: base.New(addr, protocol),
cipher: cipher,
obfsMode: obfsMode,
obfsHost: obfsHost,
Expand Down Expand Up @@ -166,5 +167,5 @@ func (pc *ssPacketConn) ReadFrom(b []byte) (int, net.Addr, error) {
}

func init() {
proxy.RegisterProtocol(Protocol, Parse)
proxy.RegisterProtocol(protocol, Parse)
}
9 changes: 5 additions & 4 deletions proxy/socks4/socks4.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,23 @@ import (
M "github.com/xjasonlyu/tun2socks/v2/metadata"
"github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal/base"
"github.com/xjasonlyu/tun2socks/v2/transport/socks4"
)

var _ proxy.Proxy = (*Socks4)(nil)

const Protocol = "socks4"
const protocol = "socks4"

type Socks4 struct {
*internal.Base
*base.Base

userID string
}

func New(addr, userID string) (*Socks4, error) {
return &Socks4{
Base: internal.New(Protocol, addr),
Base: base.New(addr, protocol),
userID: userID,
}, nil
}
Expand All @@ -51,5 +52,5 @@ func (ss *Socks4) DialContext(ctx context.Context, metadata *M.Metadata) (c net.
}

func init() {
proxy.RegisterProtocol(Protocol, Parse)
proxy.RegisterProtocol(protocol, Parse)
}
9 changes: 5 additions & 4 deletions proxy/socks5/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ import (
M "github.com/xjasonlyu/tun2socks/v2/metadata"
"github.com/xjasonlyu/tun2socks/v2/proxy"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal"
"github.com/xjasonlyu/tun2socks/v2/proxy/internal/base"
"github.com/xjasonlyu/tun2socks/v2/transport/socks5"
)

var _ proxy.Proxy = (*Socks5)(nil)

const Protocol = "socks5"
const protocol = "socks5"

type Socks5 struct {
*internal.Base
*base.Base

user string
pass string
Expand All @@ -31,7 +32,7 @@ type Socks5 struct {

func New(addr, user, pass string) (*Socks5, error) {
return &Socks5{
Base: internal.New(Protocol, addr),
Base: base.New(addr, protocol),
user: user,
pass: pass,
unix: len(addr) > 0 && addr[0] == '/',
Expand Down Expand Up @@ -198,5 +199,5 @@ func (pc *socksPacketConn) Close() error {
}

func init() {
proxy.RegisterProtocol(Protocol, Parse)
proxy.RegisterProtocol(protocol, Parse)
}
Loading

0 comments on commit a9f56af

Please sign in to comment.