This commit is contained in:
jerry901 2021-12-27 16:43:03 +09:00
parent 9f19aed89a
commit f43fe9e156
5 changed files with 168 additions and 0 deletions

5
go.mod Normal file
View File

@ -0,0 +1,5 @@
module SpoofDPI
go 1.17
require github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6

2
go.sum Normal file
View File

@ -0,0 +1,2 @@
github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6 h1:4NNbNM2Iq/k57qEu7WfL67UrbPq1uFWxW4qODCohi+0=
github.com/babolivier/go-doh-client v0.0.0-20201028162107-a76cff4cb8b6/go.mod h1:J29hk+f9lJrblVIfiJOtTFk+OblBawmib4uz/VdKzlg=

74
handler/handler.go Normal file
View File

@ -0,0 +1,74 @@
package handler
import (
"fmt"
"io/ioutil"
"log"
"net"
"SpoofDPI/util"
"github.com/babolivier/go-doh-client"
)
var resolver = doh.Resolver{
Host: "8.8.8.8",
Class: doh.IN,
}
func HandleClientRequest(clientConn net.Conn) {
defer clientConn.Close()
buf, err := util.ReadBytes(clientConn)
if err != nil {
return
}
fmt.Println("\n##### Request from client : ")
fmt.Println(string(buf))
domain, port := util.ExtractDomainAndPort(string(buf))
log.Println("domain: "+ domain)
log.Println("port: " + port)
ip, err := util.DnsLookupOverHttps(domain) // Dns lookup over https
if err != nil {
log.Fatal(err)
return
}
remoteConn, err := net.Dial("tcp", ip+":"+port) // create connection to server
if err != nil {
fmt.Println(err)
return
}
defer remoteConn.Close()
DoMitm(clientConn, remoteConn, buf)
}
func DoMitm(clientConn net.Conn, remoteConn net.Conn, data []byte) {
_, write_err := remoteConn.Write(data)
if write_err != nil {
fmt.Println("failed:", write_err)
return
}
defer remoteConn.(*net.TCPConn).CloseWrite()
buf, read_err := ioutil.ReadAll(remoteConn)
if read_err != nil {
fmt.Println("failed:", read_err)
return
}
log.Println("\n##### Response from server: ")
log.Println(string(buf))
_, write_err = clientConn.Write(buf)
if write_err != nil {
fmt.Println("failed:", write_err)
return
}
defer clientConn.(*net.TCPConn).CloseWrite()
}

28
spoof-dpi.go Normal file
View File

@ -0,0 +1,28 @@
package main
import (
"net"
"log"
"SpoofDPI/handler"
)
func main() {
log.Println("##### Listening 8080..")
listener, err := net.Listen("tcp", ":8080")
if err != nil {
panic(err)
}
for {
connClient, err := listener.Accept()
if err != nil {
log.Println("error accepting connection", err)
continue
}
log.Println("##### New connection", connClient.RemoteAddr())
go handler.HandleClientRequest(connClient)
}
}

59
util/util.go Normal file
View File

@ -0,0 +1,59 @@
package util
import (
"log"
"net"
"strings"
"io"
"github.com/babolivier/go-doh-client"
)
func ExtractDomainAndPort(s string) (string, string) {
lines := strings.Split(s, "\n")
hostPart := strings.Split(lines[1], " ")[1]
tokens := strings.Split(hostPart, ":")
if len(tokens) == 1 {
return strings.TrimSpace(string(tokens[0])), "80"
}
return string(tokens[0]), string(tokens[1])
}
func ReadBytes(conn net.Conn)([]byte, error) {
buf := make([]byte, 0, 4096) // big buffer
tmp := make([]byte, 1024) // using small tmo buffer for demonstrating
for {
n, err := conn.Read(tmp)
if err != nil {
if err != io.EOF {
log.Fatal("ReadRequest error:", err)
}
return nil, err
}
log.Println("##### got", n, "bytes.")
buf = append(buf, tmp[:n]...)
if n < 1024 {
break
}
}
return buf, nil
}
func DnsLookupOverHttps(domain string)(string, error) {
// Perform a A lookup on example.com
resolver := doh.Resolver{
Host: "8.8.8.8", // Change this with your favourite DoH-compliant resolver.
Class: doh.IN,
}
a, _, err := resolver.LookupA(domain)
if err != nil {
return "", err
}
ip := a[0].IP4
return ip, nil
}