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
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)
}

View File

@ -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
}

View File

@ -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
}

View File

@ -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) {

View File

@ -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
}
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 {
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)
}
}()
}