diff --git a/net/conn.go b/net/conn.go index 28ed435..5482731 100644 --- a/net/conn.go +++ b/net/conn.go @@ -11,27 +11,27 @@ import ( const BUF_SIZE = 1024 type Conn struct { - Conn net.Conn + conn net.Conn } -func (conn *Conn) Close() { - conn.Conn.Close() +func (c *Conn) Close() { + c.conn.Close() } -func (conn *Conn) RemoteAddr() net.Addr { - return conn.Conn.RemoteAddr() +func (c *Conn) RemoteAddr() net.Addr { + return c.conn.RemoteAddr() } -func (conn *Conn) LocalAddr() net.Addr { - return conn.Conn.LocalAddr() +func (c *Conn) LocalAddr() net.Addr { + return c.conn.LocalAddr() } -func (conn *Conn) Read(b []byte) (n int, err error) { - return conn.Conn.Read(b) +func (c *Conn) Read(b []byte) (n int, err error) { + return c.conn.Read(b) } -func (conn *Conn) Write(b []byte) (n int, err error) { - return conn.Conn.Write(b) +func (c *Conn) Write(b []byte) (n int, err error) { + return c.conn.Write(b) } func (conn *Conn) WriteChunks(c [][]byte) (n int, err error) { @@ -68,7 +68,7 @@ func (conn *Conn) ReadBytes() ([]byte, error) { } func (lConn *Conn) HandleHttp(p packet.HttpPacket) { - ip, err := doh.Lookup(p.Domain) + ip, err := doh.Lookup(p.Domain()) if err != nil { log.Debug("[HTTPS] Error looking up for domain: ", err) } @@ -96,7 +96,7 @@ func (lConn *Conn) HandleHttp(p packet.HttpPacket) { } func (lConn *Conn) HandleHttps(p packet.HttpPacket) { - ip, err := doh.Lookup(p.Domain) + ip, err := doh.Lookup(p.Domain()) if err != nil { log.Debug("[HTTPS] Error looking up for domain: ", err) } diff --git a/net/dial.go b/net/dial.go index 6710397..515f9c2 100644 --- a/net/dial.go +++ b/net/dial.go @@ -10,7 +10,7 @@ func Listen(network, address string) (Listener, error) { return Listener{}, err } - return Listener{Listener: l}, nil + return Listener{listener: l}, nil } func Dial(network, address string) (Conn, error) { @@ -19,5 +19,5 @@ func Dial(network, address string) (Conn, error) { return Conn{}, err } - return Conn{Conn: conn}, nil + return Conn{conn: conn}, nil } diff --git a/net/listener.go b/net/listener.go index a509fbf..7d99dc6 100644 --- a/net/listener.go +++ b/net/listener.go @@ -5,14 +5,14 @@ import ( ) type Listener struct { - Listener net.Listener + listener net.Listener } func (l *Listener) Accept() (Conn, error) { - conn, err := l.Listener.Accept() + conn, err := l.listener.Accept() if err != nil { return Conn{}, err } - return Conn{Conn: conn}, nil + return Conn{conn: conn}, nil } diff --git a/packet/http.go b/packet/http.go index 31fd66f..86c68a2 100644 --- a/packet/http.go +++ b/packet/http.go @@ -41,32 +41,46 @@ var validMethod = map[string]struct{}{ } type HttpPacket struct { - Raw []byte - Method string - Domain string - Version string + raw []byte + method string + domain string + version string } func NewHttpPacket(raw []byte) HttpPacket { method, domain, version := parse(raw) + return HttpPacket{ - Raw: raw, - Method: method, - Domain: domain, - Version: version, + raw: raw, + method: method, + domain: domain, + version: version, } } -func (r *HttpPacket) IsValidMethod() bool { - if _, exists := validMethod[r.Method]; exists { +func (p *HttpPacket) Raw() []byte { + return p.raw +} +func (p *HttpPacket) Method() string { + return p.method +} +func (p *HttpPacket) Domain() string { + return p.domain +} +func (p *HttpPacket) Version() string { + return p.version +} + +func (p *HttpPacket) IsValidMethod() bool { + if _, exists := validMethod[p.Method()]; exists { return true } return false } -func (r *HttpPacket) IsConnectMethod() bool { - return r.Method == "CONNECT" +func (p *HttpPacket) IsConnectMethod() bool { + return p.Method() == "CONNECT" } func parse(raw []byte) (string, string, string) { diff --git a/packet/https.go b/packet/https.go index f8ac370..dae1ba3 100644 --- a/packet/https.go +++ b/packet/https.go @@ -1,19 +1,23 @@ package packet type HttpsPacket struct { - Raw []byte + raw []byte } func NewHttpsPacket(raw []byte) HttpsPacket { return HttpsPacket{ - Raw: raw, + raw: raw, } } -func (r HttpsPacket) SplitInChunks() [][]byte { - if len(r.Raw) < 1 { - return [][]byte{r.Raw} +func (p *HttpsPacket) Raw() []byte { + return p.raw +} + +func (p *HttpsPacket) SplitInChunks() [][]byte { + if len(p.Raw()) < 1 { + return [][]byte{p.Raw()} } - return [][]byte{(r.Raw)[:1], (r.Raw)[1:]} + return [][]byte{(p.Raw())[:1], (p.Raw())[1:]} } diff --git a/proxy/proxy.go b/proxy/proxy.go index 2a63b70..83edca3 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -9,23 +9,27 @@ import ( ) type Proxy struct { - Port string + port string } func New(port string) *Proxy { return &Proxy{ - Port: port, + port: port, } } +func (p *Proxy) Port() string { + return p.port +} + func (p *Proxy) Start() { - l, err := net.Listen("tcp", ":"+p.Port) + l, err := net.Listen("tcp", ":"+p.Port()) if err != nil { log.Fatal("Error creating listener: ", err) os.Exit(1) } - log.Println("Created a listener on :", p.Port) + log.Println("Created a listener on :", p.Port()) for { conn, err := l.Accept() @@ -45,20 +49,20 @@ func (p *Proxy) Start() { } log.Debug("Client sent data: ", len(b)) - r := packet.NewHttpPacket(b) - log.Debug("New request: \n\n" + string(r.Raw)) + pkt := packet.NewHttpPacket(b) + log.Debug("New request: \n\n" + string(pkt.Raw())) - if !r.IsValidMethod() { - log.Println("Unsupported method: ", r.Method) + if !pkt.IsValidMethod() { + log.Println("Unsupported method: ", pkt.Method()) return } - if r.IsConnectMethod() { + if pkt.IsConnectMethod() { log.Debug("HTTPS Requested") - conn.HandleHttps(r) + conn.HandleHttps(pkt) } else { log.Debug("HTTP Requested.") - conn.HandleHttp(r) + conn.HandleHttp(pkt) } }() }