refactor: reformat

This commit is contained in:
xvzc 2022-11-29 16:54:28 +09:00
parent 195a68b26c
commit b552e77cbe
9 changed files with 155 additions and 156 deletions

View File

@ -27,11 +27,11 @@ func main() {
FullTimestamp: true, FullTimestamp: true,
}) })
if banner { if banner {
util.PrintColoredBanner(addr, port, dns, debug) util.PrintColoredBanner(addr, port, dns, debug)
} else { } else {
util.PrintSimpleInfo(addr, port, dns, debug) util.PrintSimpleInfo(addr, port, dns, debug)
} }
if err := util.SetOsProxy(port); err != nil { if err := util.SetOsProxy(port); err != nil {
log.Fatal(err) log.Fatal(err)

View File

@ -4,7 +4,8 @@ import (
"errors" "errors"
"sync" "sync"
"regexp" "regexp"
"github.com/babolivier/go-doh-client" "github.com/babolivier/go-doh-client"
) )
@ -16,21 +17,20 @@ func Init(dns string) {
} }
func Lookup(domain string) (string, error) { func Lookup(domain string) (string, error) {
ipRegex := "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$" ipRegex := "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"
if r, _ := regexp.MatchString(ipRegex, domain); r {
return domain, nil
}
if r, _ := regexp.MatchString(ipRegex, domain); r {
return domain, nil
}
a, _, err := resolver.LookupA(domain) a, _, err := resolver.LookupA(domain)
if err != nil { if err != nil {
return "", err return "", err
} }
if len(a) < 1 { if len(a) < 1 {
return "", errors.New(" couldn't resolve the domain") return "", errors.New(" couldn't resolve the domain")
} }
ip := a[0].IP4 ip := a[0].IP4

View File

@ -45,19 +45,19 @@ func (c *Conn) Write(b []byte) (n int, err error) {
return c.conn.Write(b) return c.conn.Write(b)
} }
func (c *Conn) SetReadDeadline(t time.Time) (error) { func (c *Conn) SetReadDeadline(t time.Time) error {
c.conn.SetReadDeadline(t) c.conn.SetReadDeadline(t)
return nil return nil
} }
func (c *Conn) SetDeadLine(t time.Time) (error) { func (c *Conn) SetDeadLine(t time.Time) error {
c.conn.SetDeadline(t) c.conn.SetDeadline(t)
return nil return nil
} }
func (c *Conn) SetKeepAlive(b bool) (error) { func (c *Conn) SetKeepAlive(b bool) error {
c.conn.SetKeepAlive(b) c.conn.SetKeepAlive(b)
return nil return nil
} }
func (conn *Conn) WriteChunks(c [][]byte) (n int, err error) { func (conn *Conn) WriteChunks(c [][]byte) (n int, err error) {
@ -75,31 +75,31 @@ func (conn *Conn) WriteChunks(c [][]byte) (n int, err error) {
} }
func (conn *Conn) ReadBytes() ([]byte, error) { func (conn *Conn) ReadBytes() ([]byte, error) {
ret := make([]byte, 0) ret := make([]byte, 0)
buf := make([]byte, BUF_SIZE) buf := make([]byte, BUF_SIZE)
for { for {
n, err := conn.Read(buf) n, err := conn.Read(buf)
if err != nil { if err != nil {
switch err.(type) { switch err.(type) {
case *net.OpError: case *net.OpError:
return nil, errors.New("timed out") return nil, errors.New("timed out")
default: default:
return nil, err return nil, err
} }
} }
ret = append(ret, buf[:n]...) ret = append(ret, buf[:n]...)
if n < BUF_SIZE { if n < BUF_SIZE {
break break
} }
} }
if len(ret) == 0 { if len(ret) == 0 {
return nil, io.EOF return nil, io.EOF
} }
return ret, nil return ret, nil
} }
func (lConn *Conn) HandleHttp(p *packet.HttpPacket) { func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {
@ -107,18 +107,18 @@ 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.Error("[HTTP DOH] Error looking up for domain with ", p.Domain() , " ", err) log.Error("[HTTP DOH] Error looking up for domain with ", p.Domain(), " ", err)
lConn.Write([]byte(p.Version() + " 502 Bad Gateway\r\n\r\n")) lConn.Write([]byte(p.Version() + " 502 Bad Gateway\r\n\r\n"))
return return
} }
log.Debug("[DOH] Found ", ip, " with ", p.Domain()) log.Debug("[DOH] Found ", ip, " with ", p.Domain())
// Create connection to server // Create connection to server
var port = "80" var port = "80"
if p.Port() != "" { if p.Port() != "" {
port = p.Port() port = p.Port()
} }
rConn, err := DialTCP("tcp", ip, port) rConn, err := DialTCP("tcp", ip, port)
if err != nil { if err != nil {
@ -126,17 +126,17 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {
return return
} }
defer func() { defer func() {
lConn.Close() lConn.Close()
log.Debug("[HTTP] Closing client Connection.. ", lConn.RemoteAddr()) log.Debug("[HTTP] Closing client Connection.. ", lConn.RemoteAddr())
rConn.Close() rConn.Close()
log.Debug("[HTTP] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr()) log.Debug("[HTTP] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr())
}() }()
log.Debug("[HTTP] New connection to the server ", p.Domain(), " ", rConn.LocalAddr()) log.Debug("[HTTP] New connection to the server ", p.Domain(), " ", rConn.LocalAddr())
go rConn.Serve(lConn, "[HTTP]", lConn.RemoteAddr().String(), p.Domain()) go rConn.Serve(lConn, "[HTTP]", lConn.RemoteAddr().String(), p.Domain())
_, err = rConn.Write(p.Raw()) _, err = rConn.Write(p.Raw())
if err != nil { if err != nil {
@ -146,7 +146,7 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {
log.Debug("[HTTP] Sent a request to ", p.Domain()) log.Debug("[HTTP] Sent a request to ", p.Domain())
lConn.Serve(rConn, "[HTTP]", lConn.RemoteAddr().String(), p.Domain()) lConn.Serve(rConn, "[HTTP]", lConn.RemoteAddr().String(), p.Domain())
} }
@ -154,17 +154,17 @@ 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.Error("[HTTPS DOH] Error looking up for domain: ", p.Domain(), " ", err) log.Error("[HTTPS DOH] Error looking up for domain: ", p.Domain(), " ", err)
lConn.Write([]byte(p.Version() + " 502 Bad Gateway\r\n\r\n")) lConn.Write([]byte(p.Version() + " 502 Bad Gateway\r\n\r\n"))
return return
} }
log.Debug("[DOH] Found ", ip, " with ", p.Domain()) log.Debug("[DOH] Found ", ip, " with ", p.Domain())
// Create a connection to the requested server // Create a connection to the requested server
var port = "443" var port = "443"
if p.Port() != "" { if p.Port() != "" {
port = p.Port() port = p.Port()
} }
rConn, err := DialTCP("tcp4", ip, port) rConn, err := DialTCP("tcp4", ip, port)
if err != nil { if err != nil {
@ -172,20 +172,20 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) {
return return
} }
defer func() { defer func() {
lConn.Close() lConn.Close()
log.Debug("[HTTPS] Closing client Connection.. ", lConn.RemoteAddr()) log.Debug("[HTTPS] Closing client Connection.. ", lConn.RemoteAddr())
rConn.Close() rConn.Close()
log.Debug("[HTTPS] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr()) log.Debug("[HTTPS] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr())
}() }()
log.Debug("[HTTPS] New connection to the server ", p.Domain(), " ", rConn.LocalAddr()) log.Debug("[HTTPS] New connection to the server ", p.Domain(), " ", rConn.LocalAddr())
_, err = lConn.Write([]byte(p.Version() + " 200 Connection Established\r\n\r\n")) _, err = lConn.Write([]byte(p.Version() + " 200 Connection Established\r\n\r\n"))
if err != nil { if err != nil {
log.Debug("[HTTPS] Error sending 200 Connection Established to the client", err) log.Debug("[HTTPS] Error sending 200 Connection Established to the client", err)
return return
} }
log.Debug("[HTTPS] Sent 200 Connection Estabalished to ", lConn.RemoteAddr()) log.Debug("[HTTPS] Sent 200 Connection Estabalished to ", lConn.RemoteAddr())
@ -194,7 +194,7 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) {
clientHello, err := lConn.ReadBytes() clientHello, err := lConn.ReadBytes()
if err != nil { if err != nil {
log.Debug("[HTTPS] Error reading client hello from the client", err) log.Debug("[HTTPS] Error reading client hello from the client", err)
return return
} }
log.Debug("[HTTPS] Client sent hello ", len(clientHello), "bytes") log.Debug("[HTTPS] Client sent hello ", len(clientHello), "bytes")
@ -205,34 +205,34 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) {
chunks := pkt.SplitInChunks() chunks := pkt.SplitInChunks()
go rConn.Serve(lConn, "[HTTPS]", rConn.RemoteAddr().String(), p.Domain()) go rConn.Serve(lConn, "[HTTPS]", rConn.RemoteAddr().String(), p.Domain())
if _, err := rConn.WriteChunks(chunks); err != nil { if _, err := rConn.WriteChunks(chunks); err != nil {
log.Debug("[HTTPS] Error writing client hello to ", p.Domain(), err) log.Debug("[HTTPS] Error writing client hello to ", p.Domain(), err)
return return
} }
lConn.Serve(rConn, "[HTTPS]", lConn.RemoteAddr().String(), p.Domain()) lConn.Serve(rConn, "[HTTPS]", lConn.RemoteAddr().String(), p.Domain())
} }
func (from *Conn) Serve(to *Conn, proto string, fd string, td string) { func (from *Conn) Serve(to *Conn, proto string, fd string, td string) {
proto += " " proto += " "
for { for {
from.conn.SetReadDeadline(time.Now().Add(2000 * time.Millisecond)) from.conn.SetReadDeadline(time.Now().Add(2000 * time.Millisecond))
buf, err := from.ReadBytes() buf, err := from.ReadBytes()
if err != nil { if err != nil {
if err == io.EOF { if err == io.EOF {
log.Debug(proto, "Finished ", fd) log.Debug(proto, "Finished ", fd)
return return
} }
log.Debug(proto, "Error reading from ", fd, " ", err) log.Debug(proto, "Error reading from ", fd, " ", err)
return return
} }
if _, err := to.Write(buf); err != nil { if _, err := to.Write(buf); err != nil {
log.Debug(proto, "Error Writing to ", td) log.Debug(proto, "Error Writing to ", td)
return return
} }
} }
} }

View File

@ -15,12 +15,12 @@ func ListenTCP(network string, addr *TCPAddr) (Listener, error) {
} }
func DialTCP(network string, ip string, port string) (*Conn, error) { func DialTCP(network string, ip string, port string) (*Conn, error) {
p, _ := strconv.Atoi(port) p, _ := strconv.Atoi(port)
addr := &net.TCPAddr{ addr := &net.TCPAddr{
IP: net.ParseIP(ip), IP: net.ParseIP(ip),
Port: p, Port: p,
} }
conn, err := net.DialTCP(network, nil, addr) conn, err := net.DialTCP(network, nil, addr)
if err != nil { if err != nil {

View File

@ -5,17 +5,16 @@ import (
) )
type TCPAddr struct { type TCPAddr struct {
Addr *net.TCPAddr Addr *net.TCPAddr
} }
func TcpAddr(ip string, port int) *TCPAddr {
addr := &net.TCPAddr{
IP: net.ParseIP(ip),
Port: port,
}
func TcpAddr(ip string, port int) (*TCPAddr) { return &TCPAddr{
addr := &net.TCPAddr { Addr: addr,
IP: net.ParseIP(ip), }
Port: port,
}
return &TCPAddr{
Addr: addr,
}
} }

View File

@ -47,7 +47,7 @@ type HttpPacket struct {
raw []byte raw []byte
method string method string
domain string domain string
port string port string
path string path string
version string version string
} }
@ -56,10 +56,10 @@ func ParseUrl(raw []byte) {
} }
func NewHttpPacket(raw []byte) (*HttpPacket, error){ func NewHttpPacket(raw []byte) (*HttpPacket, error) {
pkt := &HttpPacket{raw: raw} pkt := &HttpPacket{raw: raw}
pkt.parse() pkt.parse()
return pkt, nil return pkt, nil
} }
@ -118,40 +118,40 @@ func (p *HttpPacket) Tidy() {
result += lines[i] + "\r\n" result += lines[i] + "\r\n"
} }
result += "\r\n" result += "\r\n"
p.raw = []byte(result) p.raw = []byte(result)
} }
func (p *HttpPacket )parse() error { func (p *HttpPacket) parse() error {
reader := bufio.NewReader(strings.NewReader(string(p.raw))) reader := bufio.NewReader(strings.NewReader(string(p.raw)))
request, err := http.ReadRequest(reader) request, err := http.ReadRequest(reader)
if err != nil { if err != nil {
return err return err
} }
p.domain, p.port, err = net.SplitHostPort(request.Host) p.domain, p.port, err = net.SplitHostPort(request.Host)
if err != nil { if err != nil {
p.domain = request.Host p.domain = request.Host
p.port = "" p.port = ""
} }
p.method = request.Method p.method = request.Method
p.version = request.Proto p.version = request.Proto
p.path = request.URL.Path p.path = request.URL.Path
if request.URL.RawQuery != "" { if request.URL.RawQuery != "" {
p.path += "?" + request.URL.RawQuery p.path += "?" + request.URL.RawQuery
} }
if request.URL.RawFragment != "" { if request.URL.RawFragment != "" {
p.path += "#" + request.URL.RawFragment p.path += "#" + request.URL.RawFragment
} }
if p.path == "" { if p.path == "" {
p.path = "/" p.path = "/"
} }
request.Body.Close() request.Body.Close()
return nil return nil
} }

View File

@ -9,19 +9,19 @@ import (
) )
type Proxy struct { type Proxy struct {
addr string addr string
port int port int
} }
func New(addr string, port int) *Proxy { func New(addr string, port int) *Proxy {
return &Proxy{ return &Proxy{
addr: addr, addr: addr,
port: port, port: port,
} }
} }
func (p *Proxy) TcpAddr() *net.TCPAddr { func (p *Proxy) TcpAddr() *net.TCPAddr {
return net.TcpAddr(p.addr, p.port) return net.TcpAddr(p.addr, p.port)
} }
func (p *Proxy) Port() int { func (p *Proxy) Port() int {
@ -50,13 +50,13 @@ func (p *Proxy) Start() {
return return
} }
log.Debug("[PROXY] Request from ", conn.RemoteAddr(), "\n\n", string(b)) log.Debug("[PROXY] Request from ", conn.RemoteAddr(), "\n\n", string(b))
pkt, err := packet.NewHttpPacket(b) pkt, err := packet.NewHttpPacket(b)
if err != nil { if err != nil {
log.Debug("Error while parsing request: ", string(b)) log.Debug("Error while parsing request: ", string(b))
return return
} }
if !pkt.IsValidMethod() { if !pkt.IsValidMethod() {
log.Debug("Unsupported method: ", pkt.Method()) log.Debug("Unsupported method: ", pkt.Method())

View File

@ -18,12 +18,12 @@ func SetOsProxy(port int) error {
return err return err
} }
_, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+ "'" +strings.TrimSpace(string(network)) + "'" + " 127.0.0.1 "+ fmt.Sprint(port)).Output() _, err = exec.Command("sh", "-c", "networksetup -setwebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output()
if err != nil { if err != nil {
return err return err
} }
_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy " + "'" + strings.TrimSpace(string(network))+"'" + " 127.0.0.1 "+ fmt.Sprint(port)).Output() _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxy "+"'"+strings.TrimSpace(string(network))+"'"+" 127.0.0.1 "+fmt.Sprint(port)).Output()
if err != nil { if err != nil {
return err return err
} }
@ -41,12 +41,12 @@ func UnsetOsProxy() error {
return err return err
} }
_, err = exec.Command("sh", "-c", "networksetup -setwebproxystate " + "'" + strings.TrimSpace(string(network)) + "'" + " off").Output() _, err = exec.Command("sh", "-c", "networksetup -setwebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output()
if err != nil { if err != nil {
return err return err
} }
_, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate " + "'" + strings.TrimSpace(string(network)) + "'" + " off").Output() _, err = exec.Command("sh", "-c", "networksetup -setsecurewebproxystate "+"'"+strings.TrimSpace(string(network))+"'"+" off").Output()
if err != nil { if err != nil {
return err return err
} }

View File

@ -33,10 +33,10 @@ func PrintColoredBanner(addr string, port int, dns string, debug bool) {
} }
func PrintSimpleInfo(addr string, port int, dns string, debug bool) { func PrintSimpleInfo(addr string, port int, dns string, debug bool) {
fmt.Println("") fmt.Println("")
fmt.Println("- ADDR : ", addr) fmt.Println("- ADDR : ", addr)
fmt.Println("- PORT : ", port) fmt.Println("- PORT : ", port)
fmt.Println("- DNS : ", dns) fmt.Println("- DNS : ", dns)
fmt.Println("- DEBUG : ", debug) fmt.Println("- DEBUG : ", debug)
fmt.Println("") fmt.Println("")
} }