mirror of
https://github.com/xvzc/SpoofDPI.git
synced 2024-12-22 14:26:31 +00:00
delete unnecessary call by reference
This commit is contained in:
parent
fffe7ec168
commit
db6c30c9c3
104
packet/http.go
104
packet/http.go
@ -4,59 +4,7 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type HttpPacket struct {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *HttpPacket) IsValidMethod() bool {
|
||||
if _, exists := getValidMethods()[r.Method]; exists {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *HttpPacket) IsConnectMethod() bool {
|
||||
return r.Method == "CONNECT"
|
||||
}
|
||||
|
||||
func parse(raw *[]byte) (string, string, string) {
|
||||
var firstLine string
|
||||
for i := 0; i < len(*raw); i++ {
|
||||
if (*raw)[i] == '\n' {
|
||||
firstLine = string((*raw)[:i])
|
||||
}
|
||||
}
|
||||
|
||||
tokens := strings.Split(firstLine, " ")
|
||||
|
||||
method := strings.TrimSpace(tokens[0])
|
||||
domain := strings.TrimSpace(tokens[1])
|
||||
version := strings.TrimSpace(tokens[2])
|
||||
|
||||
domain = strings.Replace(domain, "http://", "", 1)
|
||||
domain = strings.Replace(domain, "https://", "", 1)
|
||||
domain = strings.Split(domain, ":")[0]
|
||||
domain = strings.Split(domain, "/")[0]
|
||||
|
||||
return method, domain, version
|
||||
}
|
||||
|
||||
func getValidMethods() map[string]struct{} {
|
||||
return map[string]struct{}{
|
||||
var validMethod = map[string]struct{}{
|
||||
"DELETE": {},
|
||||
"GET": {},
|
||||
"HEAD": {},
|
||||
@ -90,5 +38,55 @@ func getValidMethods() map[string]struct{} {
|
||||
"MKCALENDAR": {},
|
||||
"LINK": {},
|
||||
"UNLINK": {},
|
||||
}
|
||||
|
||||
type HttpPacket struct {
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *HttpPacket) IsValidMethod() bool {
|
||||
if _, exists := validMethod[r.Method]; exists {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (r *HttpPacket) IsConnectMethod() bool {
|
||||
return r.Method == "CONNECT"
|
||||
}
|
||||
|
||||
func parse(raw []byte) (string, string, string) {
|
||||
var firstLine string
|
||||
for i := 0; i < len(raw); i++ {
|
||||
if (raw)[i] == '\n' {
|
||||
firstLine = string((raw)[:i])
|
||||
}
|
||||
}
|
||||
|
||||
tokens := strings.Split(firstLine, " ")
|
||||
|
||||
method := strings.TrimSpace(tokens[0])
|
||||
domain := strings.TrimSpace(tokens[1])
|
||||
version := strings.TrimSpace(tokens[2])
|
||||
|
||||
domain = strings.Replace(domain, "http://", "", 1)
|
||||
domain = strings.Replace(domain, "https://", "", 1)
|
||||
domain = strings.Split(domain, ":")[0]
|
||||
domain = strings.Split(domain, "/")[0]
|
||||
|
||||
return method, domain, version
|
||||
}
|
||||
|
@ -1,19 +1,19 @@
|
||||
package packet
|
||||
|
||||
type HttpsPacket struct {
|
||||
Raw *[]byte
|
||||
Raw []byte
|
||||
}
|
||||
|
||||
func NewHttpsPacket(raw *[]byte) HttpsPacket {
|
||||
func NewHttpsPacket(raw []byte) HttpsPacket {
|
||||
return HttpsPacket{
|
||||
Raw: raw,
|
||||
}
|
||||
}
|
||||
|
||||
func (r HttpsPacket) SplitInChunks() [][]byte {
|
||||
if len(*r.Raw) < 1 {
|
||||
return [][]byte{*r.Raw}
|
||||
if len(r.Raw) < 1 {
|
||||
return [][]byte{r.Raw}
|
||||
}
|
||||
|
||||
return [][]byte{(*r.Raw)[:1], (*r.Raw)[1:]}
|
||||
return [][]byte{(r.Raw)[:1], (r.Raw)[1:]}
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ func HandleHttp(clientConn net.Conn, ip string, p *packet.HttpPacket) {
|
||||
go remoteConn.Serve(clientConn, "HTTP")
|
||||
|
||||
// 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")
|
||||
}
|
||||
@ -52,7 +52,7 @@ func HandleHttps(clientConn net.Conn, ip string, r *packet.HttpPacket) {
|
||||
// Generate a go routine that reads from the server
|
||||
go remoteConn.Serve(clientConn, "HTTPS")
|
||||
|
||||
pkt := packet.NewHttpsPacket(&clientHello)
|
||||
pkt := packet.NewHttpsPacket(clientHello)
|
||||
|
||||
chunks := pkt.SplitInChunks()
|
||||
|
||||
|
@ -20,7 +20,7 @@ func New(port string) *Proxy {
|
||||
}
|
||||
|
||||
func (p *Proxy) Start() {
|
||||
listener, 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)
|
||||
@ -29,7 +29,7 @@ func (p *Proxy) Start() {
|
||||
// util.Debug("Created a listener")
|
||||
|
||||
for {
|
||||
clientConn, err := listener.Accept()
|
||||
conn, err := l.Accept()
|
||||
if err != nil {
|
||||
log.Fatal("Error accepting connection: ", err)
|
||||
continue
|
||||
@ -38,16 +38,16 @@ func (p *Proxy) Start() {
|
||||
// util.Debug("Accepted a new connection.", clientConn.RemoteAddr())
|
||||
|
||||
go func() {
|
||||
defer clientConn.Close()
|
||||
defer conn.Close()
|
||||
|
||||
b, err := clientConn.ReadBytes()
|
||||
b, err := conn.ReadBytes()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// util.Debug("Client sent data: ", len(b))
|
||||
|
||||
r := packet.NewHttpPacket(&b)
|
||||
r := packet.NewHttpPacket(b)
|
||||
// util.Debug("Request: \n" + string(*r.Raw))
|
||||
|
||||
if !r.IsValidMethod() {
|
||||
@ -66,10 +66,10 @@ func (p *Proxy) Start() {
|
||||
|
||||
if r.IsConnectMethod() {
|
||||
// util.Debug("HTTPS Requested")
|
||||
HandleHttps(clientConn, ip, &r)
|
||||
HandleHttps(conn, ip, &r)
|
||||
} else {
|
||||
// util.Debug("HTTP Requested.")
|
||||
HandleHttp(clientConn, ip, &r)
|
||||
HandleHttp(conn, ip, &r)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user