Skip to content

Commit

Permalink
Added better verbose output
Browse files Browse the repository at this point in the history
If the verbose flag is passed the program will display what socks
proxies will be used and what the current socks/http listeners are in use.
Next, the verbose flag will show what proxy it's using each time there is
a connection
  • Loading branch information
jamesbcook committed Jul 23, 2018
1 parent ed709cb commit ee3e213
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 21 deletions.
73 changes: 53 additions & 20 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

"github.com/eahydra/socks"
"github.com/elazarl/goproxy"
"github.com/jamesbcook/print"
)

var (
Expand Down Expand Up @@ -69,59 +70,73 @@ func main() {
myFlags := flagSetup()
f, err := os.Open(myFlags.userAgentFile)
if err != nil {
log.Fatal(err)
print.Badln(err)
}
f2, err := os.Open(myFlags.socks5File)
if err != nil {
log.Fatal(err)
print.Badln(err)
}
var buf bytes.Buffer
buf.ReadFrom(f)
ua := &UserAgent{}
if err := json.Unmarshal(buf.Bytes(), ua); err != nil {
log.Fatal(err)
print.Badln(err)
}
buf.Reset()
proxies := &SocksProxy{}
buf.ReadFrom(f2)
if err := json.Unmarshal(buf.Bytes(), proxies); err != nil {
log.Fatal(err)
print.Badln(err)
}
var router socks.Dialer
proxy := goproxy.NewProxyHttpServer()
if myFlags.verbose {
router = logBuildUpStream(BuildUpstreamRouter)(proxies.Names)
proxy.ConnectDial = func(network, address string) (net.Conn, error) {
return logDialer(router.Dial)(network, address)
}
proxy.Tr.Dial = func(network, address string) (net.Conn, error) {
return logDialer(router.Dial)(network, address)
}
} else {
router = BuildUpstreamRouter(proxies.Names)
proxy.ConnectDial = func(network, address string) (net.Conn, error) {
return router.Dial(network, address)
}
proxy.Tr.Dial = func(network, address string) (net.Conn, error) {
return router.Dial(network, address)
}
}
router := BuildUpstreamRouter(proxies.Names)
socksListen, err := net.Listen("tcp", myFlags.socksListener)
if err != nil {
log.Fatal(err)
print.Badln(err)
}
if myFlags.verbose {
print.Goodf("Started socks listener on %s\n", myFlags.socksListener)
}
socksvr, err := socks.NewSocks5Server(router)
if err != nil {
log.Fatal(err)
print.Badln(err)
}
httpListen, err := net.Listen("tcp", myFlags.httpListener)
if err != nil {
log.Fatal(err)
print.Badln(err)
}
if myFlags.verbose {
print.Goodf("Started http listener on %s\n", myFlags.httpListener)
}
proxy := goproxy.NewProxyHttpServer()
proxy.OnRequest().HandleConnect(goproxy.AlwaysMitm)
proxy.OnRequest().DoFunc(
func(r *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
r.Header.Set("User-Agent", ua.randomName())
return r, nil
})
proxy.ConnectDial = func(network, address string) (net.Conn, error) {
return router.Dial(network, address)
}
proxy.Tr.Dial = func(network, address string) (net.Conn, error) {
return router.Dial(network, address)
}
if myFlags.verbose {
proxy.Verbose = true
}

go func() {
http.Serve(httpListen, proxy)
}()
print.Goodln("Ready")
if err := socksvr.Serve(socksListen); err != nil {
log.Fatal(err)
print.Badln(err)
}
}

Expand Down Expand Up @@ -150,6 +165,14 @@ func (u *UpstreamDialer) getRandomDialer() socks.Dialer {
return u.forwardDialers[randomDialer]
}

func logDialer(f func(network, address string) (net.Conn, error)) func(network, address string) (net.Conn, error) {
return func(network, address string) (net.Conn, error) {
conn, err := f(network, address)
print.Statusf("Connecting to %v\n", conn.RemoteAddr())
return conn, err
}
}

//Dial is a custom dialer that picks a random dialer before it makes it's connection
func (u *UpstreamDialer) Dial(network, address string) (net.Conn, error) {
router := u.getRandomDialer()
Expand All @@ -160,6 +183,16 @@ func (u *UpstreamDialer) Dial(network, address string) (net.Conn, error) {
return conn, nil
}

func logBuildUpStream(f func(proxies []string) socks.Dialer) func(proxies []string) socks.Dialer {
return func(proxies []string) socks.Dialer {
for x := range proxies {
print.Statusf("Loading %s\n", proxies[x])
}
defer print.Goodln("Loading complete")
return f(proxies)
}
}

//BuildUpstreamRouter populates the slice of dialers
func BuildUpstreamRouter(proxies []string) socks.Dialer {
var allForward []socks.Dialer
Expand Down
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1.0
0.2.0

0 comments on commit ee3e213

Please sign in to comment.