fedproxy/main.go

52 lines
979 B
Go
Raw Normal View History

2018-08-29 05:06:07 -07:00
package main
import (
"socks5"
"golang.org/x/net/context"
"golang.org/x/net/proxy"
"net"
"os"
"fmt"
"strings"
)
func main() {
args := os.Args[1:]
if len(args) < 2 {
fmt.Printf("usage: %s bindaddr upstreamaddr\n", os.Args[0])
return
}
upstream, err:= proxy.SOCKS5("tcp", os.Args[2], nil, nil)
if err != nil {
fmt.Printf("failed to create upstream proxy to %s, %s", os.Args[2], err.Error())
return
}
serv, err := socks5.New(&socks5.Config{
2018-08-29 05:12:07 -07:00
Dial: func(addr string) (net.Conn, error) {
2018-08-29 05:06:07 -07:00
host, _, err := net.SplitHostPort(addr)
if err != nil {
return nil, err
}
if strings.HasSuffix(host, ".onion") {
2018-08-29 05:12:07 -07:00
return upstream.Dial("tcp", addr)
2018-08-29 05:06:07 -07:00
}
2018-08-29 05:12:07 -07:00
return net.Dial("tcp", addr)
2018-08-29 05:06:07 -07:00
},
})
if err != nil {
fmt.Printf("failed to create socks proxy %s", err.Error())
return
}
l, err := net.Listen("tcp", os.Args[1])
if err != nil {
fmt.Printf("failed to listen on %s, %s", os.Args[1], err.Error())
return
}
serv.Serve(l)
}