glider/mixed.go

106 lines
1.7 KiB
Go
Raw Normal View History

2017-07-13 21:55:41 +08:00
package main
import (
"bytes"
"net"
)
// https://www.ietf.org/rfc/rfc2616.txt, http methods must be uppercase.
var httpMethods = [][]byte{
[]byte("GET"),
[]byte("POST"),
[]byte("PUT"),
[]byte("DELETE"),
[]byte("CONNECT"),
[]byte("HEAD"),
[]byte("OPTIONS"),
[]byte("TRACE"),
}
// mixedproxy
type mixedproxy struct {
*proxy
2017-07-13 21:55:41 +08:00
http Proxy
socks5 Proxy
ss Proxy
}
2017-07-15 10:41:52 +08:00
// MixedProxy returns a mixed proxy.
2017-07-13 21:55:41 +08:00
func MixedProxy(network, addr, user, pass string, upProxy Proxy) (Proxy, error) {
p := &mixedproxy{
proxy: newProxy(addr, upProxy),
2017-07-13 21:55:41 +08:00
}
p.http, _ = HTTPProxy(addr, upProxy)
p.socks5, _ = SOCKS5Proxy(network, addr, user, pass, upProxy)
if user != "" && pass != "" {
p.ss, _ = SSProxy(addr, user, pass, upProxy)
2017-07-13 21:55:41 +08:00
}
return p, nil
}
// mixedproxy .
func (p *mixedproxy) ListenAndServe() {
l, err := net.Listen("tcp", p.Addr())
if err != nil {
logf("failed to listen on %s: %v", p.Addr(), err)
return
}
logf("listening TCP on %s", p.Addr())
for {
c, err := l.Accept()
if err != nil {
logf("failed to accept: %v", err)
continue
}
go func() {
defer c.Close()
if c, ok := c.(*net.TCPConn); ok {
c.SetKeepAlive(true)
}
c := newConn(c)
if p.socks5 != nil {
head, err := c.Peek(1)
if err != nil {
logf("peek error: %s", err)
return
}
// check socks5, client send socksversion: 5 as the first byte
if head[0] == socks5Version {
p.socks5.Serve(c)
return
}
}
if p.http != nil {
head, err := c.Peek(8)
if err != nil {
logf("peek error: %s", err)
return
}
for _, method := range httpMethods {
if bytes.HasPrefix(head, method) {
p.http.Serve(c)
return
}
}
}
if p.ss != nil {
p.ss.Serve(c)
}
}()
}
}