update capsulation

This commit is contained in:
xvzc 2022-01-12 02:15:45 +09:00
parent c97539cb3f
commit fefdb5efdd
6 changed files with 69 additions and 47 deletions

View File

@ -11,27 +11,27 @@ import (
const BUF_SIZE = 1024 const BUF_SIZE = 1024
type Conn struct { type Conn struct {
Conn net.Conn conn net.Conn
} }
func (conn *Conn) Close() { func (c *Conn) Close() {
conn.Conn.Close() c.conn.Close()
} }
func (conn *Conn) RemoteAddr() net.Addr { func (c *Conn) RemoteAddr() net.Addr {
return conn.Conn.RemoteAddr() return c.conn.RemoteAddr()
} }
func (conn *Conn) LocalAddr() net.Addr { func (c *Conn) LocalAddr() net.Addr {
return conn.Conn.LocalAddr() return c.conn.LocalAddr()
} }
func (conn *Conn) Read(b []byte) (n int, err error) { func (c *Conn) Read(b []byte) (n int, err error) {
return conn.Conn.Read(b) return c.conn.Read(b)
} }
func (conn *Conn) Write(b []byte) (n int, err error) { func (c *Conn) Write(b []byte) (n int, err error) {
return conn.Conn.Write(b) return c.conn.Write(b)
} }
func (conn *Conn) WriteChunks(c [][]byte) (n int, err error) { 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) { func (lConn *Conn) HandleHttp(p packet.HttpPacket) {
ip, err := doh.Lookup(p.Domain) ip, err := doh.Lookup(p.Domain())
if err != nil { if err != nil {
log.Debug("[HTTPS] Error looking up for domain: ", err) 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) { func (lConn *Conn) HandleHttps(p packet.HttpPacket) {
ip, err := doh.Lookup(p.Domain) ip, err := doh.Lookup(p.Domain())
if err != nil { if err != nil {
log.Debug("[HTTPS] Error looking up for domain: ", err) log.Debug("[HTTPS] Error looking up for domain: ", err)
} }

View File

@ -10,7 +10,7 @@ func Listen(network, address string) (Listener, error) {
return Listener{}, err return Listener{}, err
} }
return Listener{Listener: l}, nil return Listener{listener: l}, nil
} }
func Dial(network, address string) (Conn, error) { func Dial(network, address string) (Conn, error) {
@ -19,5 +19,5 @@ func Dial(network, address string) (Conn, error) {
return Conn{}, err return Conn{}, err
} }
return Conn{Conn: conn}, nil return Conn{conn: conn}, nil
} }

View File

@ -5,14 +5,14 @@ import (
) )
type Listener struct { type Listener struct {
Listener net.Listener listener net.Listener
} }
func (l *Listener) Accept() (Conn, error) { func (l *Listener) Accept() (Conn, error) {
conn, err := l.Listener.Accept() conn, err := l.listener.Accept()
if err != nil { if err != nil {
return Conn{}, err return Conn{}, err
} }
return Conn{Conn: conn}, nil return Conn{conn: conn}, nil
} }

View File

@ -41,32 +41,46 @@ var validMethod = map[string]struct{}{
} }
type HttpPacket struct { type HttpPacket struct {
Raw []byte raw []byte
Method string method string
Domain string domain string
Version string version string
} }
func NewHttpPacket(raw []byte) HttpPacket { func NewHttpPacket(raw []byte) HttpPacket {
method, domain, version := parse(raw) method, domain, version := parse(raw)
return HttpPacket{ return HttpPacket{
Raw: raw, raw: raw,
Method: method, method: method,
Domain: domain, domain: domain,
Version: version, version: version,
} }
} }
func (r *HttpPacket) IsValidMethod() bool { func (p *HttpPacket) Raw() []byte {
if _, exists := validMethod[r.Method]; exists { 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 true
} }
return false return false
} }
func (r *HttpPacket) IsConnectMethod() bool { func (p *HttpPacket) IsConnectMethod() bool {
return r.Method == "CONNECT" return p.Method() == "CONNECT"
} }
func parse(raw []byte) (string, string, string) { func parse(raw []byte) (string, string, string) {

View File

@ -1,19 +1,23 @@
package packet package packet
type HttpsPacket struct { type HttpsPacket struct {
Raw []byte raw []byte
} }
func NewHttpsPacket(raw []byte) HttpsPacket { func NewHttpsPacket(raw []byte) HttpsPacket {
return HttpsPacket{ return HttpsPacket{
Raw: raw, raw: raw,
} }
} }
func (r HttpsPacket) SplitInChunks() [][]byte { func (p *HttpsPacket) Raw() []byte {
if len(r.Raw) < 1 { return p.raw
return [][]byte{r.Raw}
} }
return [][]byte{(r.Raw)[:1], (r.Raw)[1:]} func (p *HttpsPacket) SplitInChunks() [][]byte {
if len(p.Raw()) < 1 {
return [][]byte{p.Raw()}
}
return [][]byte{(p.Raw())[:1], (p.Raw())[1:]}
} }

View File

@ -9,23 +9,27 @@ import (
) )
type Proxy struct { type Proxy struct {
Port string port string
} }
func New(port string) *Proxy { func New(port string) *Proxy {
return &Proxy{ return &Proxy{
Port: port, port: port,
} }
} }
func (p *Proxy) Port() string {
return p.port
}
func (p *Proxy) Start() { func (p *Proxy) Start() {
l, err := net.Listen("tcp", ":"+p.Port) l, err := net.Listen("tcp", ":"+p.Port())
if err != nil { if err != nil {
log.Fatal("Error creating listener: ", err) log.Fatal("Error creating listener: ", err)
os.Exit(1) os.Exit(1)
} }
log.Println("Created a listener on :", p.Port) log.Println("Created a listener on :", p.Port())
for { for {
conn, err := l.Accept() conn, err := l.Accept()
@ -45,20 +49,20 @@ func (p *Proxy) Start() {
} }
log.Debug("Client sent data: ", len(b)) log.Debug("Client sent data: ", len(b))
r := packet.NewHttpPacket(b) pkt := packet.NewHttpPacket(b)
log.Debug("New request: \n\n" + string(r.Raw)) log.Debug("New request: \n\n" + string(pkt.Raw()))
if !r.IsValidMethod() { if !pkt.IsValidMethod() {
log.Println("Unsupported method: ", r.Method) log.Println("Unsupported method: ", pkt.Method())
return return
} }
if r.IsConnectMethod() { if pkt.IsConnectMethod() {
log.Debug("HTTPS Requested") log.Debug("HTTPS Requested")
conn.HandleHttps(r) conn.HandleHttps(pkt)
} else { } else {
log.Debug("HTTP Requested.") log.Debug("HTTP Requested.")
conn.HandleHttp(r) conn.HandleHttp(pkt)
} }
}() }()
} }