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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
// https://gist.github.com/miguelmota/7b765edff00dc676215d6174f3f30216
package main
import (
"crypto/tls"
"errors"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"time"
)
const (
defaultTimeOut = 10.
)
var (
errMalformedAdr = errors.New("malformed address")
errIPNotFound = errors.New("ip not found")
errBadFloatValue = errors.New("bad float value")
errBadConfig = errors.New("bad config")
)
func getDefaultOptions() map[string]float64 {
return map[string]float64{
"APP_CONTEXT_TIMEOUT": defaultTimeOut,
"APP_READ_HEADER_TIMEOUT": defaultTimeOut,
"APP_READ_TIMEOUT": defaultTimeOut,
"APP_WRITE_TIMEOUT": defaultTimeOut,
"APP_IDLE_TIMEOUT": defaultTimeOut,
}
}
func getIP(request *http.Request) (string, error) {
ips := request.Header.Get("X-Forwarded-For")
splitIps := strings.Split(ips, ",")
log.Println(request.RemoteAddr)
if len(splitIps) > 0 {
netIP := net.ParseIP(splitIps[len(splitIps)-1])
if netIP != nil {
return netIP.String(), nil
}
}
ip, _, err := net.SplitHostPort(request.RemoteAddr)
if err != nil {
return "", errMalformedAdr
}
if netIP := net.ParseIP(ip); netIP != nil {
ip := netIP.String()
if ip == "::1" {
return "127.0.0.1", nil
}
return ip, nil
}
return "", errIPNotFound
}
func ipHandler(writer http.ResponseWriter, request *http.Request) {
ipAddr, err := getIP(request)
if err != nil {
writer.WriteHeader(http.StatusInternalServerError)
return
}
writer.WriteHeader(http.StatusOK)
writtenLen, err := writer.Write([]byte(ipAddr))
if err != nil || writtenLen != len(ipAddr) {
writer.WriteHeader(http.StatusInternalServerError)
return
}
}
type Config struct {
Addr string
ContextTimeOut float64
ReadHeaderTimeout float64
ReadTimeout float64
WriteTimeout float64
IdleTimeout float64
}
func getConfigValue(envVarName string) (float64, error) {
defaultOptions := getDefaultOptions()
paramEnv := os.Getenv(envVarName)
if paramEnv != "" {
param, err := strconv.ParseFloat(paramEnv, 64)
if err != nil {
return defaultOptions[envVarName], errBadFloatValue
}
return param, nil
}
return defaultOptions[envVarName], nil
}
func getConfig() (Config, error) {
var config Config
var err error
appAddrEnv := os.Getenv("APP_ADDR")
if appAddrEnv != "" {
config.Addr = appAddrEnv
} else {
config.Addr = ":8080"
}
config.ContextTimeOut, err = getConfigValue("APP_CONTEXT_TIMEOUT")
if err != nil {
log.Println(err.Error())
}
config.ReadHeaderTimeout, err = getConfigValue("APP_READ_HEADER_TIMEOUT")
if err != nil {
log.Println(err.Error())
}
config.ReadTimeout, err = getConfigValue("APP_READ_TIMEOUT")
if err != nil {
log.Println(err.Error())
}
config.WriteTimeout, err = getConfigValue("APP_WRITE_TIMEOUT")
if err != nil {
log.Println(err.Error())
}
config.IdleTimeout, err = getConfigValue("APP_IDLE_TIMEOUT")
if err != nil {
log.Println(err.Error())
}
return config, err
}
func main() {
log.SetOutput(os.Stdout)
http.HandleFunc("/", ipHandler)
config, err := getConfig()
if err != nil {
log.Fatal(errBadConfig)
}
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS13,
CurvePreferences: []tls.CurveID{tls.CurveP521, tls.CurveP384, tls.CurveP256},
PreferServerCipherSuites: true,
}
server := http.Server{
Addr: config.Addr,
ReadHeaderTimeout: time.Duration(config.ReadHeaderTimeout) * time.Second,
ReadTimeout: time.Duration(config.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(config.WriteTimeout) * time.Second,
IdleTimeout: time.Duration(config.IdleTimeout) * time.Second,
TLSNextProto: nil,
ErrorLog: nil,
Handler: nil,
TLSConfig: tlsConfig,
}
log.Fatal(server.ListenAndServeTLS("/certs/server.cert", "/certs/server.key"))
}
|