diff --git a/common/conn/conn.go b/common/conn/conn.go index e33ca6e..34ff05d 100644 --- a/common/conn/conn.go +++ b/common/conn/conn.go @@ -9,29 +9,36 @@ import ( "github.com/nadoo/glider/common/log" ) +// UDPBufSize is the size of udp buffer const UDPBufSize = 65536 +// Conn struct type Conn struct { r *bufio.Reader net.Conn } +// NewConn . func NewConn(c net.Conn) Conn { return Conn{bufio.NewReader(c), c} } +// NewConnSize . func NewConnSize(c net.Conn, n int) Conn { return Conn{bufio.NewReaderSize(c, n), c} } +// Peek . func (c Conn) Peek(n int) ([]byte, error) { return c.r.Peek(n) } +// Read . func (c Conn) Read(p []byte) (int, error) { return c.r.Read(p) } +// Relay . func Relay(left, right net.Conn) (int64, int64, error) { type res struct { N int64 diff --git a/common/log/log.go b/common/log/log.go index ae80f47..edc967f 100644 --- a/common/log/log.go +++ b/common/log/log.go @@ -5,12 +5,16 @@ import "log" // Func defines a simple log function type Func func(f string, v ...interface{}) -var F Func +// F is the main log function +var F Func = func(f string, v ...interface{}) { +} +// Fatal log and exit func Fatal(v ...interface{}) { log.Fatal(v) } +// Fatalf log and exit func Fatalf(f string, v ...interface{}) { log.Fatalf(f, v) } diff --git a/common/socks/socks.go b/common/socks/socks.go index 09f9515..7532402 100644 --- a/common/socks/socks.go +++ b/common/socks/socks.go @@ -7,6 +7,7 @@ import ( "strconv" ) +// SOCKS auth type const ( AuthNone = 0 AuthPassword = 2 @@ -29,6 +30,7 @@ const ( // MaxAddrLen is the maximum size of SOCKS address in bytes. const MaxAddrLen = 1 + 1 + 255 + 2 +// Errors are socks5 errors var Errors = []error{ errors.New(""), errors.New("general failure"), @@ -42,6 +44,7 @@ var Errors = []error{ errors.New("socks5UDPAssociate"), } +// Addr . type Addr []byte // String serializes SOCKS address a to string form. @@ -73,6 +76,7 @@ func ATYP(b byte) int { return int(b &^ 0x8) } +// ReadAddrBuf reads just enough bytes from r to get a valid Addr. func ReadAddrBuf(r io.Reader, b []byte) (Addr, error) { if len(b) < MaxAddrLen { return nil, io.ErrShortBuffer diff --git a/main.go b/main.go index 6fe1c33..e9334c9 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,6 @@ package main import ( - stdlog "log" "os" "os/signal" "strings" @@ -36,11 +35,11 @@ func main() { confInit() - log.F = func(f string, v ...interface{}) { - if conf.Verbose { - stdlog.Printf(f, v...) - } - } + // log.F = func(f string, v ...interface{}) { + // if conf.Verbose { + // stdlog.Printf(f, v...) + // } + // } sDialer := NewRuleDialer(conf.rules, dialerFromConf()) diff --git a/proxy/dialer.go b/proxy/dialer.go index b25b3d1..7762b5b 100644 --- a/proxy/dialer.go +++ b/proxy/dialer.go @@ -9,7 +9,7 @@ import ( "github.com/nadoo/glider/common/log" ) -// A proxy.Dialer means to establish a connection and relay it. +// Dialer means to establish a connection and relay it. type Dialer interface { // Addr() Addr() string @@ -24,16 +24,19 @@ type Dialer interface { NextDialer(dstAddr string) Dialer } +// DialerCreator is a function to create dialers. type DialerCreator func(s string, dialer Dialer) (Dialer, error) var ( dialerMap = make(map[string]DialerCreator) ) +// RegisterDialer is used to register a dialer func RegisterDialer(name string, c DialerCreator) { dialerMap[name] = c } +// DialerFromURL calls the registered creator to create dialers. func DialerFromURL(s string, dialer Dialer) (Dialer, error) { u, err := url.Parse(s) if err != nil { diff --git a/proxy/server.go b/proxy/server.go index 3e7bd60..4bc4e1d 100644 --- a/proxy/server.go +++ b/proxy/server.go @@ -14,16 +14,19 @@ type Server interface { ListenAndServe() } +// ServerCreator is a function to create proxy servers. type ServerCreator func(s string, dialer Dialer) (Server, error) var ( serverMap = make(map[string]ServerCreator) ) +// RegisterServer is used to register a proxy server func RegisterServer(name string, c ServerCreator) { serverMap[name] = c } +// ServerFromURL calls the registered creator to create proxy servers. func ServerFromURL(s string, dialer Dialer) (Server, error) { if !strings.Contains(s, "://") { s = "mixed://" + s diff --git a/proxy/socks5/socks5.go b/proxy/socks5/socks5.go index 2d88581..a76734b 100644 --- a/proxy/socks5/socks5.go +++ b/proxy/socks5/socks5.go @@ -26,6 +26,7 @@ import ( "github.com/nadoo/glider/proxy" ) +// Version: socks5 version const Version = 5 // SOCKS5 struct