/
main.go
127 lines (112 loc) · 2.95 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"fmt"
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus/common/version"
log "github.com/sirupsen/logrus"
"io"
"net"
"net/http"
"os"
"time"
)
// App -
type App struct {
iname string
lastAddr string
}
var (
gIName = kingpin.Flag("iname", "Use given interface to discover dchp server, can be set by $MDPROXY4CS_INAME").
Envar("MDPROXY4CS_INAME").Default("eth0").String()
gLogLevel = kingpin.Flag("log-level", "Set log level, can be set $MDPROXY4CS_LOG_LEVEL").
Envar("MDPROXY4CS_LOG_LEVEL").Default("info").String()
gLogFile = kingpin.Flag("log-file", "Set log output file, - for stdout, can be set $MDPROXY4CS_LOG_FILE").
Envar("MDPROXY4CS_LOG_FILE").Default("-").String()
gHTTPListen = kingpin.Flag("http-listen", "Set server listen address, can be set by $MDPROXY4CS_HTTP_LISTEN").
Envar("MDPROXY4CS_HTTP_LISTEN").Default("169.254.169.254:39724").String()
)
// NewApp -
func NewApp() *App {
if *gLogFile == "-" {
log.SetFormatter(&log.TextFormatter{
ForceColors: true,
})
log.SetOutput(os.Stdout)
} else {
file, err := os.OpenFile(*gLogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("unable to create log file %s: %s", *gLogFile, err)
}
log.SetOutput(file)
}
lLevel, lErr := log.ParseLevel(*gLogLevel)
if lErr != nil {
lLevel = log.ErrorLevel
}
log.SetLevel(lLevel)
return &App{
iname: *gIName,
lastAddr: "0.0.0.0",
}
}
// ServeHTTP
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Infof("serving request %s...", r.URL.Path)
var (
attempt = 0
res *http.Response
err error
)
url := fmt.Sprintf("http://%s%s", a.getVrouterAddress(), r.URL.Path)
for {
res, err = http.Get(url)
if err == nil {
break
}
log.Errorf("could not request %s (attempt %d / 3): %s", url, attempt+1, err)
if attempt == 2 {
w.WriteHeader(500)
return
}
attempt++
time.Sleep(time.Second * 5)
}
content, err := io.ReadAll(res.Body)
if err != nil {
log.Errorf("cannot read reponse from %s: %s", url, err)
w.WriteHeader(500)
return
}
log.Debugf("serving response to %s: %s", r.URL.Path, string(content))
w.WriteHeader(res.StatusCode)
w.Write(content)
}
func (a *App) getVrouterAddress() string {
log.Info("discovering dhcp address...")
ifname, err := net.InterfaceByName(a.iname)
if err != nil {
log.Errorf("got unknown interface %s: %s", a.iname, err)
return a.lastAddr
}
client := Client{Iface: ifname}
ip, err := client.DiscoverServer()
if err != nil {
log.Printf("error: unable to discover dhcp server: %s", err)
return a.lastAddr
}
log.Infof("found dhcp address: %s", ip.String())
return ip.String()
}
func main() {
kingpin.Version(version.Print("mdproxy4cs"))
kingpin.HelpFlag.Short('h')
kingpin.Parse()
app := NewApp()
log.Infof("listening on %s", *gHTTPListen)
if err := http.ListenAndServe(*gHTTPListen, app); err != nil {
panic(err)
}
}
// Local Variables:
// ispell-local-dictionary: "american"
// End: