log: add a default implement of func F

This commit is contained in:
nadoo 2018-06-26 17:00:13 +08:00
parent 3adf3b30b5
commit 36246d9d08
7 changed files with 29 additions and 8 deletions

View File

@ -9,29 +9,36 @@ import (
"github.com/nadoo/glider/common/log" "github.com/nadoo/glider/common/log"
) )
// UDPBufSize is the size of udp buffer
const UDPBufSize = 65536 const UDPBufSize = 65536
// Conn struct
type Conn struct { type Conn struct {
r *bufio.Reader r *bufio.Reader
net.Conn net.Conn
} }
// NewConn .
func NewConn(c net.Conn) Conn { func NewConn(c net.Conn) Conn {
return Conn{bufio.NewReader(c), c} return Conn{bufio.NewReader(c), c}
} }
// NewConnSize .
func NewConnSize(c net.Conn, n int) Conn { func NewConnSize(c net.Conn, n int) Conn {
return Conn{bufio.NewReaderSize(c, n), c} return Conn{bufio.NewReaderSize(c, n), c}
} }
// Peek .
func (c Conn) Peek(n int) ([]byte, error) { func (c Conn) Peek(n int) ([]byte, error) {
return c.r.Peek(n) return c.r.Peek(n)
} }
// Read .
func (c Conn) Read(p []byte) (int, error) { func (c Conn) Read(p []byte) (int, error) {
return c.r.Read(p) return c.r.Read(p)
} }
// Relay .
func Relay(left, right net.Conn) (int64, int64, error) { func Relay(left, right net.Conn) (int64, int64, error) {
type res struct { type res struct {
N int64 N int64

View File

@ -5,12 +5,16 @@ import "log"
// Func defines a simple log function // Func defines a simple log function
type Func func(f string, v ...interface{}) 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{}) { func Fatal(v ...interface{}) {
log.Fatal(v) log.Fatal(v)
} }
// Fatalf log and exit
func Fatalf(f string, v ...interface{}) { func Fatalf(f string, v ...interface{}) {
log.Fatalf(f, v) log.Fatalf(f, v)
} }

View File

@ -7,6 +7,7 @@ import (
"strconv" "strconv"
) )
// SOCKS auth type
const ( const (
AuthNone = 0 AuthNone = 0
AuthPassword = 2 AuthPassword = 2
@ -29,6 +30,7 @@ const (
// MaxAddrLen is the maximum size of SOCKS address in bytes. // MaxAddrLen is the maximum size of SOCKS address in bytes.
const MaxAddrLen = 1 + 1 + 255 + 2 const MaxAddrLen = 1 + 1 + 255 + 2
// Errors are socks5 errors
var Errors = []error{ var Errors = []error{
errors.New(""), errors.New(""),
errors.New("general failure"), errors.New("general failure"),
@ -42,6 +44,7 @@ var Errors = []error{
errors.New("socks5UDPAssociate"), errors.New("socks5UDPAssociate"),
} }
// Addr .
type Addr []byte type Addr []byte
// String serializes SOCKS address a to string form. // String serializes SOCKS address a to string form.
@ -73,6 +76,7 @@ func ATYP(b byte) int {
return int(b &^ 0x8) 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) { func ReadAddrBuf(r io.Reader, b []byte) (Addr, error) {
if len(b) < MaxAddrLen { if len(b) < MaxAddrLen {
return nil, io.ErrShortBuffer return nil, io.ErrShortBuffer

11
main.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
stdlog "log"
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
@ -36,11 +35,11 @@ func main() {
confInit() confInit()
log.F = func(f string, v ...interface{}) { // log.F = func(f string, v ...interface{}) {
if conf.Verbose { // if conf.Verbose {
stdlog.Printf(f, v...) // stdlog.Printf(f, v...)
} // }
} // }
sDialer := NewRuleDialer(conf.rules, dialerFromConf()) sDialer := NewRuleDialer(conf.rules, dialerFromConf())

View File

@ -9,7 +9,7 @@ import (
"github.com/nadoo/glider/common/log" "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 { type Dialer interface {
// Addr() // Addr()
Addr() string Addr() string
@ -24,16 +24,19 @@ type Dialer interface {
NextDialer(dstAddr string) Dialer NextDialer(dstAddr string) Dialer
} }
// DialerCreator is a function to create dialers.
type DialerCreator func(s string, dialer Dialer) (Dialer, error) type DialerCreator func(s string, dialer Dialer) (Dialer, error)
var ( var (
dialerMap = make(map[string]DialerCreator) dialerMap = make(map[string]DialerCreator)
) )
// RegisterDialer is used to register a dialer
func RegisterDialer(name string, c DialerCreator) { func RegisterDialer(name string, c DialerCreator) {
dialerMap[name] = c dialerMap[name] = c
} }
// DialerFromURL calls the registered creator to create dialers.
func DialerFromURL(s string, dialer Dialer) (Dialer, error) { func DialerFromURL(s string, dialer Dialer) (Dialer, error) {
u, err := url.Parse(s) u, err := url.Parse(s)
if err != nil { if err != nil {

View File

@ -14,16 +14,19 @@ type Server interface {
ListenAndServe() ListenAndServe()
} }
// ServerCreator is a function to create proxy servers.
type ServerCreator func(s string, dialer Dialer) (Server, error) type ServerCreator func(s string, dialer Dialer) (Server, error)
var ( var (
serverMap = make(map[string]ServerCreator) serverMap = make(map[string]ServerCreator)
) )
// RegisterServer is used to register a proxy server
func RegisterServer(name string, c ServerCreator) { func RegisterServer(name string, c ServerCreator) {
serverMap[name] = c serverMap[name] = c
} }
// ServerFromURL calls the registered creator to create proxy servers.
func ServerFromURL(s string, dialer Dialer) (Server, error) { func ServerFromURL(s string, dialer Dialer) (Server, error) {
if !strings.Contains(s, "://") { if !strings.Contains(s, "://") {
s = "mixed://" + s s = "mixed://" + s

View File

@ -26,6 +26,7 @@ import (
"github.com/nadoo/glider/proxy" "github.com/nadoo/glider/proxy"
) )
// Version: socks5 version
const Version = 5 const Version = 5
// SOCKS5 struct // SOCKS5 struct