simple DNS response works
This commit is contained in:
parent
ad75934761
commit
ab5d8dec9e
3 changed files with 64 additions and 4 deletions
10
main.go
10
main.go
|
|
@ -54,6 +54,8 @@ type NodeConfig struct {
|
||||||
|
|
||||||
RPCEndpoints []string `xml:"rpc"`
|
RPCEndpoints []string `xml:"rpc"`
|
||||||
|
|
||||||
|
DNS []string `xml:"dns"`
|
||||||
|
|
||||||
Log LogConfig `xml:"log"`
|
Log LogConfig `xml:"log"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -276,6 +278,14 @@ func nodeMain() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add dns server frontends
|
||||||
|
for _, dnsListen := range config.DNS {
|
||||||
|
if err := node.AddDnsFrontend(dnsListen); err != nil {
|
||||||
|
log.Criticalf("failed to spawn dns %s: %v\n", dnsListen, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// start node
|
// start node
|
||||||
node.Start()
|
node.Start()
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,56 @@
|
||||||
package node
|
package node
|
||||||
|
|
||||||
import "net"
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"net"
|
||||||
|
)
|
||||||
|
|
||||||
type LuxDnsServer struct {
|
type LuxDnsServer struct {
|
||||||
node *LuxNode
|
node *LuxNode
|
||||||
stopChan chan bool
|
stopChan chan bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLuxDnsServer(node *LuxNode) LuxDnsServer {
|
func NewLuxDnsServer(node *LuxNode) *LuxDnsServer {
|
||||||
return LuxDnsServer{
|
return &LuxDnsServer{
|
||||||
node: node,
|
node: node,
|
||||||
stopChan: make(chan bool),
|
stopChan: make(chan bool),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var NO = binary.BigEndian
|
||||||
|
|
||||||
func (sv *LuxDnsServer) HandleRequest(req []byte) []byte {
|
func (sv *LuxDnsServer) HandleRequest(req []byte) []byte {
|
||||||
return nil
|
id := NO.Uint16(req[0:2])
|
||||||
|
flags := NO.Uint16(req[2:4])
|
||||||
|
qdcount := NO.Uint16(req[4:6])
|
||||||
|
ancount := NO.Uint16(req[6:8])
|
||||||
|
nscount := NO.Uint16(req[8:10])
|
||||||
|
arcount := NO.Uint16(req[10:12])
|
||||||
|
data := req[12:]
|
||||||
|
|
||||||
|
qr := flags >> 15
|
||||||
|
opcode := (flags >> 11) & 0b1111
|
||||||
|
var rcode uint16
|
||||||
|
|
||||||
|
if qr != 0 || opcode != 0 {
|
||||||
|
// throw not supported
|
||||||
|
goto reply
|
||||||
|
}
|
||||||
|
|
||||||
|
qr = 1
|
||||||
|
rcode = 4
|
||||||
|
reply:
|
||||||
|
flags |= (qr << 15) | rcode
|
||||||
|
|
||||||
|
hdr := make([]byte, 12)
|
||||||
|
NO.PutUint16(hdr[0:2], id)
|
||||||
|
NO.PutUint16(hdr[2:4], flags)
|
||||||
|
NO.PutUint16(hdr[4:6], qdcount)
|
||||||
|
NO.PutUint16(hdr[6:8], ancount)
|
||||||
|
NO.PutUint16(hdr[8:10], nscount)
|
||||||
|
NO.PutUint16(hdr[10:12], arcount)
|
||||||
|
|
||||||
|
return append(hdr, data...)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sv *LuxDnsServer) CreateFrontend(udpListen string) error {
|
func (sv *LuxDnsServer) CreateFrontend(udpListen string) error {
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,8 @@ type LuxNode struct {
|
||||||
stateLock sync.RWMutex
|
stateLock sync.RWMutex
|
||||||
|
|
||||||
genlist net.LuxNonceList
|
genlist net.LuxNonceList
|
||||||
|
|
||||||
|
dns *LuxDnsServer
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewLuxNode(nodeKey crypto.LuxKey, ks crypto.LuxKeyStore) LuxNode {
|
func NewLuxNode(nodeKey crypto.LuxKey, ks crypto.LuxKeyStore) LuxNode {
|
||||||
|
|
@ -34,6 +36,7 @@ func NewLuxNode(nodeKey crypto.LuxKey, ks crypto.LuxKeyStore) LuxNode {
|
||||||
neighbors: make(map[proto.LuxID]*ipnet.UDPAddr),
|
neighbors: make(map[proto.LuxID]*ipnet.UDPAddr),
|
||||||
state: NewLuxNodeState(),
|
state: NewLuxNodeState(),
|
||||||
genlist: net.NewLuxNonceList(),
|
genlist: net.NewLuxNonceList(),
|
||||||
|
dns: nil,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -81,6 +84,14 @@ func (node *LuxNode) AddNeighbor(id proto.LuxID, udpAddr string) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (node *LuxNode) AddDnsFrontend(udpListen string) error {
|
||||||
|
if node.dns == nil {
|
||||||
|
node.dns = NewLuxDnsServer(node)
|
||||||
|
}
|
||||||
|
|
||||||
|
return node.dns.CreateFrontend(udpListen)
|
||||||
|
}
|
||||||
|
|
||||||
func (node *LuxNode) GetState() *LuxNodeState {
|
func (node *LuxNode) GetState() *LuxNodeState {
|
||||||
return &node.state
|
return &node.state
|
||||||
}
|
}
|
||||||
|
|
@ -228,6 +239,10 @@ func (node *LuxNode) Start() {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (node *LuxNode) Stop() {
|
func (node *LuxNode) Stop() {
|
||||||
|
if node.dns != nil {
|
||||||
|
node.dns.Stop()
|
||||||
|
}
|
||||||
|
|
||||||
node.running = false
|
node.running = false
|
||||||
node.router.Stop()
|
node.router.Stop()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue