Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve dev tools (dns and webserver) #30

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ start-webserver_%: ## Start the webserver (usage: make start-webserver_<namespac
NAMESPACE=`echo $* | cut -d- -f1` && \
PORT=`echo $* | cut -d- -f2` && \
echo "Starting webserver in namespace ns-host-$$NAMESPACE on port $$PORT..." && \
cd tools/web && sudo ip netns exec ns-host-$$NAMESPACE ./webserver host-$$NAMESPACE $$PORT
# cd tools/web && sudo ip netns exec ns-host-$* ./webserver host-$* 80
cd tools/web && sudo ip netns exec ns-host-$$NAMESPACE ./webserver --host host-$$NAMESPACE --port $$PORT

start-controller: ## Start the controller
@echo "Starting controller..."
Expand Down
9 changes: 9 additions & 0 deletions tools/dns/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
debugging: true
hosts:
- domain_name: wb.hawk.net
ipv6_addresses:
- fcbb:cc00:2::a
- domain_name: wc.hawk.net
ipv6_addresses:
- fcbb:cc00:3::a
Binary file modified tools/dns/dns
Binary file not shown.
Binary file removed tools/dns/dnsv2
Binary file not shown.
27 changes: 26 additions & 1 deletion tools/dns/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,29 @@ module github.com/hawkv6/hawkwings/tools/dns

go 1.20

require github.com/phuslu/fastdns v0.8.1
require (
github.com/phuslu/fastdns v0.8.1
github.com/spf13/viper v1.17.0
)

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sys v0.12.0 // indirect
golang.org/x/text v0.13.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
491 changes: 491 additions & 0 deletions tools/dns/go.sum

Large diffs are not rendered by default.

90 changes: 68 additions & 22 deletions tools/dns/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,49 @@ package main

import (
"bytes"
"flag"
"fmt"
"log"
"net/netip"

"github.com/phuslu/fastdns"
"github.com/spf13/viper"
)

var (
viperInstance = viper.NewWithOptions(viper.KeyDelimiter("\\"))
Params Config
CfgFile string
)

type Host struct {
DomainName string `mapstructure:"domain_name"`
Ipv6Addresses []string `mapstructure:"ipv6_addresses"`
}

type Config struct {
Debugging bool `mapstructure:"debugging"`
Hosts []Host `mapstructure:"hosts"`
}

func ParseConfig() error {
if len(viperInstance.ConfigFileUsed()) != 0 {
if err := viperInstance.ReadInConfig(); err != nil {
return fmt.Errorf("failed to load config file %s: %v", viperInstance.ConfigFileUsed(), err)
}
}

if err := viperInstance.Unmarshal(&Params); err != nil {
return fmt.Errorf("failed to unmarshal config: %v", err)
}

return nil
}

func GetConfigInstance() *viper.Viper {
return viperInstance
}

type DNSHandler struct {
Debug bool
}
Expand All @@ -17,36 +54,30 @@ func (h *DNSHandler) ServeDNS(rw fastdns.ResponseWriter, req *fastdns.Message) {
log.Printf("%s: CLASS %s TYPE %s\n", req.Domain, req.Question.Class, req.Question.Type)
}

hostB := []byte("wb.hawk.net")
hostC := []byte("wc.hawk.net")
if bytes.Equal(req.Domain, hostB) {
switch req.Question.Type {
case fastdns.TypeA:
fastdns.HOST(rw, req, 60, []netip.Addr{netip.MustParseAddr("fcbb:cc00:2::a")})
case fastdns.TypeAAAA:
fastdns.HOST(rw, req, 60, []netip.Addr{netip.MustParseAddr("fcbb:cc00:2::a")})
default:
fastdns.Error(rw, req, fastdns.RcodeNXDomain)
}
} else if bytes.Equal(req.Domain, hostC) {
switch req.Question.Type {
case fastdns.TypeA:
fastdns.HOST(rw, req, 60, []netip.Addr{netip.MustParseAddr("fcbb:cc00:3::a")})
case fastdns.TypeAAAA:
fastdns.HOST(rw, req, 60, []netip.Addr{netip.MustParseAddr("fcbb:cc00:3::a")})
default:
fastdns.Error(rw, req, fastdns.RcodeNXDomain)
for _, host := range Params.Hosts {
if bytes.Equal(req.Domain, []byte(host.DomainName)) {
switch req.Question.Type {
case fastdns.TypeA:
for _, addr := range host.Ipv6Addresses {
fastdns.HOST(rw, req, 60, []netip.Addr{netip.MustParseAddr(addr)})
}
case fastdns.TypeAAAA:
for _, addr := range host.Ipv6Addresses {
fastdns.HOST(rw, req, 60, []netip.Addr{netip.MustParseAddr(addr)})
}
default:
fastdns.Error(rw, req, fastdns.RcodeNXDomain)
}
}
}
}

func main() {
func Start() {
addr := ":53"

server := &fastdns.ForkServer{
Handler: &DNSHandler{
// Debug: os.Getenv("DEBUG") != "",
Debug: true,
Debug: Params.Debugging,
},
Stats: &fastdns.CoreStats{
Prefix: "coredns_",
Expand All @@ -63,3 +94,18 @@ func main() {
log.Fatalf("dnsserver error: %+v", err)
}
}

func init() {
flag.StringVar(&CfgFile, "config", "./config.yaml", "config file (default is ./config.yaml)")
}

func main() {
flag.Parse()
viperInstance.SetConfigFile(CfgFile)
err := ParseConfig()
if err != nil {
log.Fatalf("failed to parse config: %v", err)
}

Start()
}
2 changes: 2 additions & 0 deletions tools/web/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/hawkv6/hawkwings/tools/web

go 1.20

require github.com/mattes/go-asciibot v0.0.0-20190603170252-3fa6d766c482
2 changes: 2 additions & 0 deletions tools/web/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/mattes/go-asciibot v0.0.0-20190603170252-3fa6d766c482 h1:Is74U2kXPdMV6wu/Z1QYiFB8SrNvhFx9EK7ZS/4i5kM=
github.com/mattes/go-asciibot v0.0.0-20190603170252-3fa6d766c482/go.mod h1:akTvhl4803od3DOIWgnTKgOJx3Pevvt7BU9pRrKdRVA=
37 changes: 22 additions & 15 deletions tools/web/main.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,37 @@
package main

import (
"flag"
"fmt"
"net/http"
"os"

"github.com/mattes/go-asciibot"
)

func main() {
if len(os.Args) < 3 {
fmt.Println("Usage: go run main.go <host-b|host-c> <port>")
return
var (
host string
port string
)

func init() {
flag.Usage = func() {
fmt.Println("Usage: web [options]")
flag.PrintDefaults()
}
flag.StringVar(&host, "host", "host-b", "host name which should be displayed")
flag.StringVar(&port, "port", "8080", "port number to listen on")
}

host := os.Args[1]
port := os.Args[2]
func generateMessage(host string) string {
image := asciibot.Random()
return fmt.Sprintf("Welcome from %s\n%s", host, image)
}

func main() {
flag.Parse()
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Received request from", r.RemoteAddr)
switch host {
case "host-b":
fmt.Fprintln(w, "Welcome from Host-B")
case "host-c":
fmt.Fprintln(w, "Welcome from Host-C")
default:
http.Error(w, "Invalid host", http.StatusBadRequest)
}
fmt.Fprintln(w, generateMessage(host))
})

fmt.Printf("Starting server on host %s at port %s\n", host, port)
Expand Down
Binary file modified tools/web/webserver
Binary file not shown.
Loading