Skip to content

Commit

Permalink
one port support socks5/http proxy protol
Browse files Browse the repository at this point in the history
  • Loading branch information
ls0f committed Apr 24, 2017
1 parent d2dc174 commit 40253bd
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 125 deletions.
65 changes: 22 additions & 43 deletions cracker/http_connect/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,48 @@ import (
"io"
"net"

"net/url"
"strings"

"github.com/lovedboy/cracker/cracker/logger"
"github.com/lovedboy/cracker/cracker/proxy"
)

var g = logger.GetLogger()

type httpConnect struct {
raddr string
secret string
wait chan bool
type HttpConnect struct {
Raddr string
Secret string
}

func (h *httpConnect) handleConn(conn net.Conn) {
func (h *HttpConnect) HandleConn(msg []byte, conn net.Conn) {

defer conn.Close()
buf := make([]byte, 1024)
n, err := conn.Read(buf)
var method, addr, host string
fmt.Sscanf(string(msg[:bytes.IndexByte(msg, '\n')]), "%s%s", &method, &host)
hostPortURL, err := url.Parse(host)
if err != nil {
g.Errorf("read err:%s", err)
g.Error(err)
return
}
var method, addr string
fmt.Sscanf(string(buf[:bytes.IndexByte(buf[:n], '\n')]), "%s%s", &method, &addr)
if strings.Index(hostPortURL.Host, ":") == -1 {
if hostPortURL.Opaque == "443" {
addr = hostPortURL.Scheme + ":443"
} else {
addr = hostPortURL.Host + ":80"
}
} else {
addr = hostPortURL.Host
}
g.Debugf("will connect %s ... ", addr)
lp, err := proxy.Connect(h.raddr, addr, h.secret)
lp, err := proxy.Connect(h.Raddr, addr, h.Secret)
if err != nil {
g.Errorf("proxy connect err:%s", err)
return
}
if method == "CONNECT" {
conn.Write([]byte("HTTP/1.1 200 Connection established\r\n\r\n")) //响应客户端连接成功
} else {
conn.Write(buf[:n])
lp.Write(msg)
}
//进行转发
go func() {
Expand All @@ -50,34 +59,4 @@ func (h *httpConnect) handleConn(conn net.Conn) {
}()
io.Copy(lp, conn)
lp.Close()
g.Debugf("close connection with %s", conn.RemoteAddr().String())

}

func (h *httpConnect) Wait() {
<-h.wait
}

func NewHttpConnect(addr, raddr, secret string) (h *httpConnect, err error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
g.Infof("http proxy listen at:[%s]", addr)
h = &httpConnect{
raddr: raddr,
secret: secret,
wait: make(chan bool),
}
go func() {
for {
conn, err := l.Accept()
if err != nil {
g.Errorf("accept err:%s", err)
}
go h.handleConn(conn)

}
}()
return h, nil
}
65 changes: 65 additions & 0 deletions cracker/local_server/local_proxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package local_server

import (
"net"

"github.com/lovedboy/cracker/cracker/http_connect"
"github.com/lovedboy/cracker/cracker/logger"
"github.com/lovedboy/cracker/cracker/socks"
)

var g = logger.GetLogger()

type localProxyServer struct {
socks5 *socks.Socks5
http *http_connect.HttpConnect
wait chan bool
}

func (s *localProxyServer) handleConn(conn net.Conn) {

defer conn.Close()
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
g.Errorf("read err:%s", err)
return
}

if buf[0] == 0x04 {
g.Warning("not support socks4")
} else if buf[0] == 0x05 {
s.socks5.HandleConn(buf[:n], conn)
} else {
s.http.HandleConn(buf[:n], conn)
}
g.Debugf("close connection with %s", conn.RemoteAddr().String())

}

func (s *localProxyServer) Wait() {
<-s.wait
}

func NewlocalProxyServer(addr, raddr, secret string) (s *localProxyServer, err error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
g.Infof("socks5/http proxy listen at:[%s]", addr)
s = &localProxyServer{
socks5: &socks.Socks5{Raddr: raddr, Secret: secret},
http: &http_connect.HttpConnect{Raddr: raddr, Secret: secret},
}
go func() {
for {
conn, err := l.Accept()
if err != nil {
g.Errorf("accept err:%s", err)
}
go s.handleConn(conn)

}
}()
return s, nil
}
61 changes: 7 additions & 54 deletions cracker/socks/socks5.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,17 @@ import (

var g = logger.GetLogger()

type socks5 struct {
raddr string
secret string
wait chan bool
type Socks5 struct {
Raddr string
Secret string
}

func (s *socks5) handleConn(conn net.Conn) {
func (s *Socks5) HandleConn(msg []byte, conn net.Conn) {

defer conn.Close()
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
g.Errorf("read err:%s", err)
return
}

if buf[0] != 0x05 {
//只处理Socks5协议
g.Warning("only support sock5...\n")
return
}
//客户端回应:Socks服务端不需要验证方式
conn.Write([]byte{0x05, 0x00})
n, _ = conn.Read(buf)
buf := make([]byte, 1024)
n, _ := conn.Read(buf)
var host, port string
switch buf[3] {
case 0x01: //IP V4
Expand All @@ -47,13 +34,9 @@ func (s *socks5) handleConn(conn net.Conn) {
port = strconv.Itoa(int(buf[n-2])<<8 | int(buf[n-1]))

addr := net.JoinHostPort(host, port)
if err != nil {
g.Errorf("host port join err:%s", err)
return
}

g.Debugf("will connect %s ... ", addr)
lp, err := proxy.Connect(s.raddr, addr, s.secret)
lp, err := proxy.Connect(s.Raddr, addr, s.Secret)
if err != nil {
g.Errorf("proxy connect err:%s", err)
return
Expand All @@ -69,34 +52,4 @@ func (s *socks5) handleConn(conn net.Conn) {
}()
io.Copy(lp, conn)
lp.Close()
g.Debugf("close connection with %s", conn.RemoteAddr().String())

}

func (s *socks5) Wait() {
<-s.wait
}

func NewSocks5(addr, raddr, secret string) (s *socks5, err error) {
l, err := net.Listen("tcp", addr)
if err != nil {
return nil, err
}
g.Infof("socks5 proxy listen at:[%s]", addr)
s = &socks5{
raddr: raddr,
secret: secret,
wait: make(chan bool),
}
go func() {
for {
conn, err := l.Accept()
if err != nil {
g.Errorf("accept err:%s", err)
}
go s.handleConn(conn)

}
}()
return s, nil
}
34 changes: 6 additions & 28 deletions local/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ import (
"flag"
"fmt"
"os"
"sync"

"github.com/lovedboy/cracker/cracker/http_connect"
. "github.com/lovedboy/cracker/cracker/local_server"
"github.com/lovedboy/cracker/cracker/logger"
"github.com/lovedboy/cracker/cracker/proxy"
"github.com/lovedboy/cracker/cracker/socks"
)

var (
Expand All @@ -20,8 +18,7 @@ var (
var g = logger.GetLogger()

func main() {
socks5Addr := flag.String("socks5", "127.0.0.1:1080", "socks5 listen addr")
httpAddr := flag.String("http", "127.0.0.1:8080", "http listen addr")
addr := flag.String("addr", "127.0.0.1:1080", "listen addr")
raddr := flag.String("raddr", "", "remote http url(e.g, https://example.com)")
secret := flag.String("secret", "", "secret key")
debug := flag.Bool("debug", false, "debug mode")
Expand All @@ -35,28 +32,9 @@ func main() {
}
logger.InitLogger(*debug)
proxy.Init()
wg := &sync.WaitGroup{}
if *socks5Addr != "" {
s, err := socks.NewSocks5(*socks5Addr, *raddr, *secret)
if err != nil {
g.Fatal(err)
}
wg.Add(1)
go func() {
s.Wait()
wg.Done()
}()
s, err := NewlocalProxyServer(*addr, *raddr, *secret)
if err != nil {
g.Fatal(err)
}
if *httpAddr != "" {
h, err := http_connect.NewHttpConnect(*httpAddr, *raddr, *secret)
if err != nil {
g.Fatal(err)
}
wg.Add(1)
go func() {
h.Wait()
wg.Done()
}()
}
wg.Wait()
s.Wait()
}

0 comments on commit 40253bd

Please sign in to comment.