delete unnecessary call by reference

This commit is contained in:
xvzc 2022-01-11 04:27:12 +09:00
parent fffe7ec168
commit db6c30c9c3
4 changed files with 57 additions and 59 deletions

View File

@ -4,14 +4,50 @@ import (
"strings" "strings"
) )
var validMethod = map[string]struct{}{
"DELETE": {},
"GET": {},
"HEAD": {},
"POST": {},
"PUT": {},
"CONNECT": {},
"OPTIONS": {},
"TRACE": {},
"COPY": {},
"LOCK": {},
"MKCOL": {},
"MOVE": {},
"PROPFIND": {},
"PROPPATCH": {},
"SEARCH": {},
"UNLOCK": {},
"BIND": {},
"REBIND": {},
"UNBIND": {},
"ACL": {},
"REPORT": {},
"MKACTIVITY": {},
"CHECKOUT": {},
"MERGE": {},
"M-SEARCH": {},
"NOTIFY": {},
"SUBSCRIBE": {},
"UNSUBSCRIBE": {},
"PATCH": {},
"PURGE": {},
"MKCALENDAR": {},
"LINK": {},
"UNLINK": {},
}
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,
@ -22,7 +58,7 @@ func NewHttpPacket(raw *[]byte) HttpPacket {
} }
func (r *HttpPacket) IsValidMethod() bool { func (r *HttpPacket) IsValidMethod() bool {
if _, exists := getValidMethods()[r.Method]; exists { if _, exists := validMethod[r.Method]; exists {
return true return true
} }
@ -33,11 +69,11 @@ func (r *HttpPacket) IsConnectMethod() bool {
return r.Method == "CONNECT" return r.Method == "CONNECT"
} }
func parse(raw *[]byte) (string, string, string) { func parse(raw []byte) (string, string, string) {
var firstLine string var firstLine string
for i := 0; i < len(*raw); i++ { for i := 0; i < len(raw); i++ {
if (*raw)[i] == '\n' { if (raw)[i] == '\n' {
firstLine = string((*raw)[:i]) firstLine = string((raw)[:i])
} }
} }
@ -54,41 +90,3 @@ func parse(raw *[]byte) (string, string, string) {
return method, domain, version return method, domain, version
} }
func getValidMethods() map[string]struct{} {
return map[string]struct{}{
"DELETE": {},
"GET": {},
"HEAD": {},
"POST": {},
"PUT": {},
"CONNECT": {},
"OPTIONS": {},
"TRACE": {},
"COPY": {},
"LOCK": {},
"MKCOL": {},
"MOVE": {},
"PROPFIND": {},
"PROPPATCH": {},
"SEARCH": {},
"UNLOCK": {},
"BIND": {},
"REBIND": {},
"UNBIND": {},
"ACL": {},
"REPORT": {},
"MKACTIVITY": {},
"CHECKOUT": {},
"MERGE": {},
"M-SEARCH": {},
"NOTIFY": {},
"SUBSCRIBE": {},
"UNSUBSCRIBE": {},
"PATCH": {},
"PURGE": {},
"MKCALENDAR": {},
"LINK": {},
"UNLINK": {},
}
}

View File

@ -1,19 +1,19 @@
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 (r HttpsPacket) SplitInChunks() [][]byte {
if len(*r.Raw) < 1 { if len(r.Raw) < 1 {
return [][]byte{*r.Raw} return [][]byte{r.Raw}
} }
return [][]byte{(*r.Raw)[:1], (*r.Raw)[1:]} return [][]byte{(r.Raw)[:1], (r.Raw)[1:]}
} }

View File

@ -20,7 +20,7 @@ func HandleHttp(clientConn net.Conn, ip string, p *packet.HttpPacket) {
go remoteConn.Serve(clientConn, "HTTP") go remoteConn.Serve(clientConn, "HTTP")
// util.Debug("[HTTP] Sending request to the server") // util.Debug("[HTTP] Sending request to the server")
fmt.Fprintf(remoteConn.Conn, string(*p.Raw)) fmt.Fprintf(remoteConn.Conn, string(p.Raw))
go clientConn.Serve(remoteConn, "HTTP") go clientConn.Serve(remoteConn, "HTTP")
} }
@ -52,7 +52,7 @@ func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpPacket) {
// Generate a go routine that reads from the server // Generate a go routine that reads from the server
go remoteConn.Serve(clientConn, "HTTPS") go remoteConn.Serve(clientConn, "HTTPS")
pkt := packet.NewHttpsPacket(&clientHello) pkt := packet.NewHttpsPacket(clientHello)
chunks := pkt.SplitInChunks() chunks := pkt.SplitInChunks()

View File

@ -20,7 +20,7 @@ func New(port string) *Proxy {
} }
func (p *Proxy) Start() { func (p *Proxy) Start() {
listener, 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)
@ -29,7 +29,7 @@ func (p *Proxy) Start() {
// util.Debug("Created a listener") // util.Debug("Created a listener")
for { for {
clientConn, err := listener.Accept() conn, err := l.Accept()
if err != nil { if err != nil {
log.Fatal("Error accepting connection: ", err) log.Fatal("Error accepting connection: ", err)
continue continue
@ -38,16 +38,16 @@ func (p *Proxy) Start() {
// util.Debug("Accepted a new connection.", clientConn.RemoteAddr()) // util.Debug("Accepted a new connection.", clientConn.RemoteAddr())
go func() { go func() {
defer clientConn.Close() defer conn.Close()
b, err := clientConn.ReadBytes() b, err := conn.ReadBytes()
if err != nil { if err != nil {
return return
} }
// util.Debug("Client sent data: ", len(b)) // util.Debug("Client sent data: ", len(b))
r := packet.NewHttpPacket(&b) r := packet.NewHttpPacket(b)
// util.Debug("Request: \n" + string(*r.Raw)) // util.Debug("Request: \n" + string(*r.Raw))
if !r.IsValidMethod() { if !r.IsValidMethod() {
@ -66,10 +66,10 @@ func (p *Proxy) Start() {
if r.IsConnectMethod() { if r.IsConnectMethod() {
// util.Debug("HTTPS Requested") // util.Debug("HTTPS Requested")
HandleHttps(clientConn, ip, &r) HandleHttps(conn, ip, &r)
} else { } else {
// util.Debug("HTTP Requested.") // util.Debug("HTTP Requested.")
HandleHttp(clientConn, ip, &r) HandleHttp(conn, ip, &r)
} }
}() }()
} }