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"
)
// 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

View File

@ -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)
}

View File

@ -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

11
main.go
View File

@ -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())

View File

@ -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 {

View File

@ -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

View File

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