Update to match latest go version.
This commit is contained in:
parent
a55aed58cb
commit
ffb93ccd7e
53
main.go
53
main.go
|
@ -1,14 +1,24 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"go.megvii-inc.com/transparent-proxy/server"
|
||||
"flag"
|
||||
"fmt"
|
||||
"net"
|
||||
"git.eve.moe/jackyyf/transparent-proxy/server"
|
||||
"io"
|
||||
"net"
|
||||
)
|
||||
|
||||
var port = flag.Int("port", 1080, "Listen port of the server")
|
||||
|
||||
func main() {
|
||||
proxy_listener, err := server.NewIPv4TransparentListener(":9091")
|
||||
flag.Parse()
|
||||
go IPv4Handler()
|
||||
go IPv6Handler()
|
||||
select {}
|
||||
}
|
||||
|
||||
func IPv4Handler() {
|
||||
proxy_listener, err := server.NewIPv4TransparentListener(fmt.Sprintf("0.0.0.0:%d", *port))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
@ -18,8 +28,24 @@ func main() {
|
|||
fmt.Println("Accept Error: ", err.Error())
|
||||
continue
|
||||
}
|
||||
fmt.Printf("Accepted connection: %s => %s\n", conn.TCPConn().RemoteAddr().String(), conn.RealAddr().String())
|
||||
go handle(conn)
|
||||
// fmt.Printf("Accepted connection: %s => %s\n", conn.TCPConn().RemoteAddr().String(), conn.RealAddr().String())
|
||||
go handle4(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func IPv6Handler() {
|
||||
proxy_listener, err := server.NewIPv6TransparentListener(fmt.Sprintf("[::]:%d", *port))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for {
|
||||
conn, err := proxy_listener.Accept()
|
||||
if err != nil {
|
||||
fmt.Println("Accept Error: ", err.Error())
|
||||
continue
|
||||
}
|
||||
// fmt.Printf("Accepted connection: %s => %s\n", conn.TCPConn().RemoteAddr().String(), conn.RealAddr().String())
|
||||
go handle6(conn)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -36,7 +62,7 @@ func pipeThenClose(reader io.Reader, writer io.WriteCloser) {
|
|||
}
|
||||
}
|
||||
if err == io.EOF {
|
||||
fmt.Println("Reader reached EOF, closing.")
|
||||
// fmt.Println("Reader reached EOF, closing.")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
|
@ -47,13 +73,26 @@ func pipeThenClose(reader io.Reader, writer io.WriteCloser) {
|
|||
}
|
||||
}
|
||||
|
||||
func handle(conn server.TransparentConnection) {
|
||||
func handle4(conn server.TransparentConnection) {
|
||||
rconn, err := net.DialTCP("tcp4", nil, conn.RealAddr())
|
||||
if err != nil {
|
||||
fmt.Println("Connect to ", conn.RealAddr().String(), " error: ", err.Error())
|
||||
conn.TCPConn().Close()
|
||||
return
|
||||
}
|
||||
// fmt.Printf("ESTABLISHED %s <=> %s\n", conn.TCPConn().RemoteAddr().String(), conn.RealAddr().String())
|
||||
go pipeThenClose(conn.TCPConn(), rconn)
|
||||
go pipeThenClose(rconn, conn.TCPConn())
|
||||
}
|
||||
|
||||
func handle6(conn server.TransparentConnection) {
|
||||
rconn, err := net.DialTCP("tcp6", conn.TCPConn().RemoteAddr().(*net.TCPAddr), conn.RealAddr())
|
||||
if err != nil {
|
||||
fmt.Println("Connect to ", conn.RealAddr().String(), " error: ", err.Error())
|
||||
conn.TCPConn().Close()
|
||||
return
|
||||
}
|
||||
// fmt.Printf("ESTABLISHED %s <=> %s\n", conn.TCPConn().RemoteAddr().String(), conn.RealAddr().String())
|
||||
go pipeThenClose(conn.TCPConn(), rconn)
|
||||
go pipeThenClose(rconn, conn.TCPConn())
|
||||
}
|
|
@ -9,9 +9,9 @@ package server
|
|||
// WARZONE BEGINS HERE! MIND YOUR HEAD!
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"net"
|
||||
"golang.org/x/sys/unix"
|
||||
"net"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
const (
|
||||
|
@ -19,13 +19,14 @@ const (
|
|||
)
|
||||
|
||||
func GetFDFromTCPConn(conn *net.TCPConn) int {
|
||||
// Actual fd is stored at: (*(*TCPConn).conn.fd).sysfd
|
||||
// Actual fd is stored at: (*(*TCPConn).conn.fd).pfd.Sysfd
|
||||
v := reflect.ValueOf(*conn)
|
||||
c := v.FieldByName("conn")
|
||||
fdp := c.FieldByName("fd")
|
||||
fd := reflect.Indirect(fdp)
|
||||
sysfd := fd.FieldByName("sysfd")
|
||||
return int(sysfd.Int())
|
||||
pfd := fd.FieldByName("pfd")
|
||||
Sysfd := pfd.FieldByName("Sysfd")
|
||||
return int(Sysfd.Int())
|
||||
}
|
||||
|
||||
func GetAddr4FromFD(fd int) *net.TCPAddr {
|
||||
|
@ -43,7 +44,7 @@ func GetAddr4FromFD(fd int) *net.TCPAddr {
|
|||
Scope_id 4byte ignore for ipv4
|
||||
*/
|
||||
addr.Flowinfo = be32toh(addr.Flowinfo)
|
||||
a, b, c, d := byte(addr.Flowinfo >> 24), byte((addr.Flowinfo >> 16) & 255), byte((addr.Flowinfo >> 8) & 255), byte(addr.Flowinfo & 255)
|
||||
a, b, c, d := byte(addr.Flowinfo>>24), byte((addr.Flowinfo>>16)&255), byte((addr.Flowinfo>>8)&255), byte(addr.Flowinfo&255)
|
||||
ip := net.IPv4(a, b, c, d)
|
||||
return &net.TCPAddr{IP: ip, Port: int(be16toh(addr.Port)), Zone: ""}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue