mirror of
https://github.com/xvzc/SpoofDPI.git
synced 2024-12-22 06:15:51 +00:00
refactor: reformat
This commit is contained in:
parent
195a68b26c
commit
b552e77cbe
@ -27,11 +27,11 @@ func main() {
|
||||
FullTimestamp: true,
|
||||
})
|
||||
|
||||
if banner {
|
||||
util.PrintColoredBanner(addr, port, dns, debug)
|
||||
} else {
|
||||
util.PrintSimpleInfo(addr, port, dns, debug)
|
||||
}
|
||||
if banner {
|
||||
util.PrintColoredBanner(addr, port, dns, debug)
|
||||
} else {
|
||||
util.PrintSimpleInfo(addr, port, dns, debug)
|
||||
}
|
||||
|
||||
if err := util.SetOsProxy(port); err != nil {
|
||||
log.Fatal(err)
|
||||
|
18
doh/dns.go
18
doh/dns.go
@ -4,7 +4,8 @@ import (
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"regexp"
|
||||
"regexp"
|
||||
|
||||
"github.com/babolivier/go-doh-client"
|
||||
)
|
||||
|
||||
@ -16,21 +17,20 @@ func Init(dns string) {
|
||||
}
|
||||
|
||||
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]?)$"
|
||||
|
||||
if r, _ := regexp.MatchString(ipRegex, domain); r {
|
||||
return domain, nil
|
||||
}
|
||||
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
|
||||
}
|
||||
|
||||
a, _, err := resolver.LookupA(domain)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(a) < 1 {
|
||||
return "", errors.New(" couldn't resolve the domain")
|
||||
}
|
||||
if len(a) < 1 {
|
||||
return "", errors.New(" couldn't resolve the domain")
|
||||
}
|
||||
|
||||
ip := a[0].IP4
|
||||
|
||||
|
158
net/conn.go
158
net/conn.go
@ -45,19 +45,19 @@ func (c *Conn) Write(b []byte) (n int, err error) {
|
||||
return c.conn.Write(b)
|
||||
}
|
||||
|
||||
func (c *Conn) SetReadDeadline(t time.Time) (error) {
|
||||
c.conn.SetReadDeadline(t)
|
||||
return nil
|
||||
func (c *Conn) SetReadDeadline(t time.Time) error {
|
||||
c.conn.SetReadDeadline(t)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) SetDeadLine(t time.Time) (error) {
|
||||
c.conn.SetDeadline(t)
|
||||
return nil
|
||||
func (c *Conn) SetDeadLine(t time.Time) error {
|
||||
c.conn.SetDeadline(t)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Conn) SetKeepAlive(b bool) (error) {
|
||||
c.conn.SetKeepAlive(b)
|
||||
return nil
|
||||
func (c *Conn) SetKeepAlive(b bool) error {
|
||||
c.conn.SetKeepAlive(b)
|
||||
return nil
|
||||
}
|
||||
|
||||
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) {
|
||||
ret := make([]byte, 0)
|
||||
buf := make([]byte, BUF_SIZE)
|
||||
ret := make([]byte, 0)
|
||||
buf := make([]byte, BUF_SIZE)
|
||||
|
||||
for {
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *net.OpError:
|
||||
return nil, errors.New("timed out")
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
ret = append(ret, buf[:n]...)
|
||||
for {
|
||||
n, err := conn.Read(buf)
|
||||
if err != nil {
|
||||
switch err.(type) {
|
||||
case *net.OpError:
|
||||
return nil, errors.New("timed out")
|
||||
default:
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
ret = append(ret, buf[:n]...)
|
||||
|
||||
if n < BUF_SIZE {
|
||||
break
|
||||
}
|
||||
}
|
||||
if n < BUF_SIZE {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(ret) == 0 {
|
||||
return nil, io.EOF
|
||||
}
|
||||
if len(ret) == 0 {
|
||||
return nil, io.EOF
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {
|
||||
@ -107,18 +107,18 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {
|
||||
|
||||
ip, err := doh.Lookup(p.Domain())
|
||||
if err != nil {
|
||||
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"))
|
||||
return
|
||||
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"))
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug("[DOH] Found ", ip, " with ", p.Domain())
|
||||
|
||||
// Create connection to server
|
||||
var port = "80"
|
||||
if p.Port() != "" {
|
||||
port = p.Port()
|
||||
}
|
||||
var port = "80"
|
||||
if p.Port() != "" {
|
||||
port = p.Port()
|
||||
}
|
||||
|
||||
rConn, err := DialTCP("tcp", ip, port)
|
||||
if err != nil {
|
||||
@ -126,17 +126,17 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
lConn.Close()
|
||||
log.Debug("[HTTP] Closing client Connection.. ", lConn.RemoteAddr())
|
||||
defer func() {
|
||||
lConn.Close()
|
||||
log.Debug("[HTTP] Closing client Connection.. ", lConn.RemoteAddr())
|
||||
|
||||
rConn.Close()
|
||||
log.Debug("[HTTP] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr())
|
||||
}()
|
||||
rConn.Close()
|
||||
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())
|
||||
if err != nil {
|
||||
@ -146,7 +146,7 @@ func (lConn *Conn) HandleHttp(p *packet.HttpPacket) {
|
||||
|
||||
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())
|
||||
if err != nil {
|
||||
log.Error("[HTTPS DOH] Error looking up for domain: ", p.Domain(), " ", err)
|
||||
lConn.Write([]byte(p.Version() + " 502 Bad Gateway\r\n\r\n"))
|
||||
return
|
||||
lConn.Write([]byte(p.Version() + " 502 Bad Gateway\r\n\r\n"))
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug("[DOH] Found ", ip, " with ", p.Domain())
|
||||
|
||||
// Create a connection to the requested server
|
||||
var port = "443"
|
||||
if p.Port() != "" {
|
||||
port = p.Port()
|
||||
}
|
||||
var port = "443"
|
||||
if p.Port() != "" {
|
||||
port = p.Port()
|
||||
}
|
||||
|
||||
rConn, err := DialTCP("tcp4", ip, port)
|
||||
if err != nil {
|
||||
@ -172,20 +172,20 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) {
|
||||
return
|
||||
}
|
||||
|
||||
defer func() {
|
||||
lConn.Close()
|
||||
log.Debug("[HTTPS] Closing client Connection.. ", lConn.RemoteAddr())
|
||||
defer func() {
|
||||
lConn.Close()
|
||||
log.Debug("[HTTPS] Closing client Connection.. ", lConn.RemoteAddr())
|
||||
|
||||
rConn.Close()
|
||||
log.Debug("[HTTPS] Closing server Connection.. ", p.Domain(), " ", rConn.LocalAddr())
|
||||
}()
|
||||
rConn.Close()
|
||||
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"))
|
||||
if err != nil {
|
||||
log.Debug("[HTTPS] Error sending 200 Connection Established to the client", err)
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
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()
|
||||
if err != nil {
|
||||
log.Debug("[HTTPS] Error reading client hello from the client", err)
|
||||
return
|
||||
return
|
||||
}
|
||||
|
||||
log.Debug("[HTTPS] Client sent hello ", len(clientHello), "bytes")
|
||||
@ -205,34 +205,34 @@ func (lConn *Conn) HandleHttps(p *packet.HttpPacket) {
|
||||
|
||||
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 {
|
||||
log.Debug("[HTTPS] Error writing client hello to ", p.Domain(), err)
|
||||
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) {
|
||||
proto += " "
|
||||
|
||||
for {
|
||||
from.conn.SetReadDeadline(time.Now().Add(2000 * time.Millisecond))
|
||||
buf, err := from.ReadBytes()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
log.Debug(proto, "Finished ", fd)
|
||||
return
|
||||
}
|
||||
log.Debug(proto, "Error reading from ", fd, " ", err)
|
||||
return
|
||||
}
|
||||
for {
|
||||
from.conn.SetReadDeadline(time.Now().Add(2000 * time.Millisecond))
|
||||
buf, err := from.ReadBytes()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
log.Debug(proto, "Finished ", fd)
|
||||
return
|
||||
}
|
||||
log.Debug(proto, "Error reading from ", fd, " ", err)
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := to.Write(buf); err != nil {
|
||||
log.Debug(proto, "Error Writing to ", td)
|
||||
return
|
||||
}
|
||||
}
|
||||
if _, err := to.Write(buf); err != nil {
|
||||
log.Debug(proto, "Error Writing to ", td)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
10
net/dial.go
10
net/dial.go
@ -15,12 +15,12 @@ func ListenTCP(network string, addr *TCPAddr) (Listener, error) {
|
||||
}
|
||||
|
||||
func DialTCP(network string, ip string, port string) (*Conn, error) {
|
||||
p, _ := strconv.Atoi(port)
|
||||
p, _ := strconv.Atoi(port)
|
||||
|
||||
addr := &net.TCPAddr{
|
||||
IP: net.ParseIP(ip),
|
||||
Port: p,
|
||||
}
|
||||
addr := &net.TCPAddr{
|
||||
IP: net.ParseIP(ip),
|
||||
Port: p,
|
||||
}
|
||||
|
||||
conn, err := net.DialTCP(network, nil, addr)
|
||||
if err != nil {
|
||||
|
19
net/tcp.go
19
net/tcp.go
@ -5,17 +5,16 @@ import (
|
||||
)
|
||||
|
||||
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) {
|
||||
addr := &net.TCPAddr {
|
||||
IP: net.ParseIP(ip),
|
||||
Port: port,
|
||||
}
|
||||
|
||||
return &TCPAddr{
|
||||
Addr: addr,
|
||||
}
|
||||
return &TCPAddr{
|
||||
Addr: addr,
|
||||
}
|
||||
}
|
||||
|
@ -47,7 +47,7 @@ type HttpPacket struct {
|
||||
raw []byte
|
||||
method string
|
||||
domain string
|
||||
port string
|
||||
port string
|
||||
path string
|
||||
version string
|
||||
}
|
||||
@ -56,10 +56,10 @@ func ParseUrl(raw []byte) {
|
||||
|
||||
}
|
||||
|
||||
func NewHttpPacket(raw []byte) (*HttpPacket, error){
|
||||
pkt := &HttpPacket{raw: raw}
|
||||
func NewHttpPacket(raw []byte) (*HttpPacket, error) {
|
||||
pkt := &HttpPacket{raw: raw}
|
||||
|
||||
pkt.parse()
|
||||
pkt.parse()
|
||||
|
||||
return pkt, nil
|
||||
}
|
||||
@ -118,40 +118,40 @@ func (p *HttpPacket) Tidy() {
|
||||
result += lines[i] + "\r\n"
|
||||
}
|
||||
|
||||
result += "\r\n"
|
||||
result += "\r\n"
|
||||
|
||||
p.raw = []byte(result)
|
||||
}
|
||||
|
||||
func (p *HttpPacket )parse() error {
|
||||
reader := bufio.NewReader(strings.NewReader(string(p.raw)))
|
||||
request, err := http.ReadRequest(reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
func (p *HttpPacket) parse() error {
|
||||
reader := bufio.NewReader(strings.NewReader(string(p.raw)))
|
||||
request, err := http.ReadRequest(reader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.domain, p.port, err = net.SplitHostPort(request.Host)
|
||||
if err != nil {
|
||||
p.domain = request.Host
|
||||
p.port = ""
|
||||
}
|
||||
p.domain, p.port, err = net.SplitHostPort(request.Host)
|
||||
if err != nil {
|
||||
p.domain = request.Host
|
||||
p.port = ""
|
||||
}
|
||||
|
||||
p.method = request.Method
|
||||
p.version = request.Proto
|
||||
p.path = request.URL.Path
|
||||
p.method = request.Method
|
||||
p.version = request.Proto
|
||||
p.path = request.URL.Path
|
||||
|
||||
if request.URL.RawQuery != "" {
|
||||
p.path += "?" + request.URL.RawQuery
|
||||
}
|
||||
if request.URL.RawQuery != "" {
|
||||
p.path += "?" + request.URL.RawQuery
|
||||
}
|
||||
|
||||
if request.URL.RawFragment != "" {
|
||||
p.path += "#" + request.URL.RawFragment
|
||||
}
|
||||
if p.path == "" {
|
||||
p.path = "/"
|
||||
}
|
||||
if request.URL.RawFragment != "" {
|
||||
p.path += "#" + request.URL.RawFragment
|
||||
}
|
||||
if p.path == "" {
|
||||
p.path = "/"
|
||||
}
|
||||
|
||||
request.Body.Close()
|
||||
request.Body.Close()
|
||||
|
||||
return nil
|
||||
return nil
|
||||
}
|
||||
|
@ -9,19 +9,19 @@ import (
|
||||
)
|
||||
|
||||
type Proxy struct {
|
||||
addr string
|
||||
addr string
|
||||
port int
|
||||
}
|
||||
|
||||
func New(addr string, port int) *Proxy {
|
||||
return &Proxy{
|
||||
addr: addr,
|
||||
addr: addr,
|
||||
port: port,
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
@ -50,13 +50,13 @@ func (p *Proxy) Start() {
|
||||
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)
|
||||
if err != nil {
|
||||
log.Debug("Error while parsing request: ", string(b))
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
log.Debug("Error while parsing request: ", string(b))
|
||||
return
|
||||
}
|
||||
|
||||
if !pkt.IsValidMethod() {
|
||||
log.Debug("Unsupported method: ", pkt.Method())
|
||||
|
@ -18,12 +18,12 @@ func SetOsProxy(port int) error {
|
||||
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 {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
@ -41,12 +41,12 @@ func UnsetOsProxy() error {
|
||||
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 {
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
12
util/util.go
12
util/util.go
@ -33,10 +33,10 @@ func PrintColoredBanner(addr string, port int, dns string, debug bool) {
|
||||
}
|
||||
|
||||
func PrintSimpleInfo(addr string, port int, dns string, debug bool) {
|
||||
fmt.Println("")
|
||||
fmt.Println("- ADDR : ", addr)
|
||||
fmt.Println("- PORT : ", port)
|
||||
fmt.Println("- DNS : ", dns)
|
||||
fmt.Println("- DEBUG : ", debug)
|
||||
fmt.Println("")
|
||||
fmt.Println("")
|
||||
fmt.Println("- ADDR : ", addr)
|
||||
fmt.Println("- PORT : ", port)
|
||||
fmt.Println("- DNS : ", dns)
|
||||
fmt.Println("- DEBUG : ", debug)
|
||||
fmt.Println("")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user