mirror of
https://github.com/cunnie/sslip.io.git
synced 2025-10-07 16:41:19 +08:00
sslip.io: lean main.go
Moved much of the processing of DNS messages into the library. Testing a library is easier than testing `main`, so I like to keep a lean `main`.
This commit is contained in:
23
src/main.go
23
src/main.go
@@ -1,12 +1,10 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"github.com/cunnie/sslip.io/src/xip"
|
"github.com/cunnie/sslip.io/src/xip"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
)
|
)
|
||||||
import "net"
|
|
||||||
import "golang.org/x/net/dns/dnsmessage"
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
conn, err := net.ListenUDP("udp", &net.UDPAddr{Port: 53})
|
conn, err := net.ListenUDP("udp", &net.UDPAddr{Port: 53})
|
||||||
@@ -14,29 +12,20 @@ func main() {
|
|||||||
log.Fatal(err.Error())
|
log.Fatal(err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
var m dnsmessage.Message
|
query := make([]byte, 512)
|
||||||
buf := make([]byte, 512)
|
|
||||||
|
|
||||||
for {
|
for {
|
||||||
_, _, err := conn.ReadFromUDP(buf)
|
_, addr, err := conn.ReadFromUDP(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
err = m.Unpack(buf)
|
|
||||||
|
response, err := xip.QueryResponse(query)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err.Error())
|
log.Println(err.Error())
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
for _, question := range m.Questions {
|
_, err = conn.WriteToUDP(response, addr)
|
||||||
jsonQuestion, err := json.Marshal(question)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err.Error())
|
|
||||||
}
|
|
||||||
answer, _ := xip.NameToA(question.GoString())
|
|
||||||
jsonAnswer, err := json.Marshal(answer)
|
|
||||||
log.Println(jsonQuestion)
|
|
||||||
log.Println(jsonAnswer)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,10 @@
|
|||||||
package xip
|
package xip
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"golang.org/x/net/dns/dnsmessage"
|
"golang.org/x/net/dns/dnsmessage"
|
||||||
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -12,6 +14,26 @@ import (
|
|||||||
var ipv4RE= regexp.MustCompile(`(^|[.-])(((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])[.-]){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))($|[.-])`)
|
var ipv4RE= regexp.MustCompile(`(^|[.-])(((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])[.-]){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))($|[.-])`)
|
||||||
var ipv6RE= regexp.MustCompile(`(^|[.-])(([0-9a-fA-F]{1,4}-){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}-){1,7}-|([0-9a-fA-F]{1,4}-){1,6}-[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}-){1,5}(-[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}-){1,4}(-[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}-){1,3}(-[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}-){1,2}(-[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}-((-[0-9a-fA-F]{1,4}){1,6})|-((-[0-9a-fA-F]{1,4}){1,7}|-)|fe80-(-[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|--(ffff(-0{1,4}){0,1}-){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}-){1,4}-((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))($|[.-])`)
|
var ipv6RE= regexp.MustCompile(`(^|[.-])(([0-9a-fA-F]{1,4}-){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}-){1,7}-|([0-9a-fA-F]{1,4}-){1,6}-[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}-){1,5}(-[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}-){1,4}(-[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}-){1,3}(-[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}-){1,2}(-[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}-((-[0-9a-fA-F]{1,4}){1,6})|-((-[0-9a-fA-F]{1,4}){1,7}|-)|fe80-(-[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|--(ffff(-0{1,4}){0,1}-){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}-){1,4}-((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))($|[.-])`)
|
||||||
|
|
||||||
|
func QueryResponse(query []byte) ([]byte, error) {
|
||||||
|
var m dnsmessage.Message
|
||||||
|
|
||||||
|
err := m.Unpack(query)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
for _, question := range m.Questions {
|
||||||
|
jsonQuestion, err := json.Marshal(question)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
answer, _ := NameToA(question.GoString())
|
||||||
|
jsonAnswer, err := json.Marshal(answer)
|
||||||
|
log.Println(jsonQuestion)
|
||||||
|
log.Println(jsonAnswer)
|
||||||
|
}
|
||||||
|
return []byte{0x0}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func NameToA (fqdnString string) (dnsmessage.AResource, error) {
|
func NameToA (fqdnString string) (dnsmessage.AResource, error) {
|
||||||
fqdn:=[]byte(fqdnString)
|
fqdn:=[]byte(fqdnString)
|
||||||
if ! ipv4RE.Match(fqdn) {
|
if ! ipv4RE.Match(fqdn) {
|
||||||
|
Reference in New Issue
Block a user