update
This commit is contained in:
2
.dockerignore
Normal file
2
.dockerignore
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
go.sum
|
||||||
|
echo
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1,3 @@
|
|||||||
echo.exe
|
echo.exe
|
||||||
|
nohup.out
|
||||||
|
echo
|
||||||
|
35
Dockerfile
Normal file
35
Dockerfile
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
FROM reg.charlienet.top/go/golang:1.22-alpine as builder
|
||||||
|
|
||||||
|
ARG app_name=echo
|
||||||
|
RUN echo $app_name
|
||||||
|
|
||||||
|
ENV APPNAME=echo
|
||||||
|
RUN echo $app_name
|
||||||
|
|
||||||
|
WORKDIR /build
|
||||||
|
|
||||||
|
RUN echo $app_name
|
||||||
|
|
||||||
|
COPY . .
|
||||||
|
|
||||||
|
ENV GOPROXY https://goproxy.cn,direct
|
||||||
|
RUN go mod tidy && go build -ldflags="-s -w"
|
||||||
|
|
||||||
|
FROM alpine:3.19
|
||||||
|
|
||||||
|
# 使用阿里云镜像
|
||||||
|
# RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
|
||||||
|
# RUN apk update --no-cache && apk add --no-cache ca-certificates tzdata
|
||||||
|
ENV TZ Asia/Shanghai
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
COPY --from=builder /build/echo echo
|
||||||
|
|
||||||
|
# COPY conf conf
|
||||||
|
|
||||||
|
# ENV GIN_MODE=release
|
||||||
|
# ENV PATH /app:$PATH
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
CMD ["./echo"]
|
58
main.go
58
main.go
@ -3,11 +3,16 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var name string
|
var (
|
||||||
|
name string
|
||||||
|
RemoteIPHeaders = []string{"X-Forwarded-For", "X-Real-IP"}
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
hostname, err := os.Hostname()
|
hostname, err := os.Hostname()
|
||||||
@ -28,9 +33,56 @@ func main() {
|
|||||||
|
|
||||||
func echoHandler(w http.ResponseWriter, r *http.Request) {
|
func echoHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
clientIp := getRemoteIP(r)
|
clientIp := getRemoteIP(r)
|
||||||
fmt.Fprintf(w, "hello %s Host %s provides services", clientIp, name)
|
|
||||||
|
log.Printf("client %s connected\n\n", clientIp)
|
||||||
|
fmt.Fprintf(w, "hello %s, Host %s provides services\n", clientIp, name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getRemoteIP(r *http.Request) string {
|
func getRemoteIP(r *http.Request) string {
|
||||||
return r.RemoteAddr
|
for k, v := range r.Header {
|
||||||
|
fmt.Printf("KEY=%s VALUE=%v\n", k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, headerName := range RemoteIPHeaders {
|
||||||
|
ip, valid := validateHeader(r.Header.Get(headerName))
|
||||||
|
if valid {
|
||||||
|
return ip
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return RemoteIP(r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateHeader(header string) (clientIP string, valid bool) {
|
||||||
|
if header == "" {
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
items := strings.Split(header, ",")
|
||||||
|
for i := len(items) - 1; i >= 0; i-- {
|
||||||
|
ipStr := strings.TrimSpace(items[i])
|
||||||
|
ip := net.ParseIP(ipStr)
|
||||||
|
if ip == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
if i == 0 {
|
||||||
|
return ipStr, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// // X-Forwarded-For is appended by proxy
|
||||||
|
// // Check IPs in reverse order and stop when find untrusted proxy
|
||||||
|
// if (i == 0) || (!engine.isTrustedProxy(ip)) {
|
||||||
|
// return ipStr, true
|
||||||
|
// }
|
||||||
|
}
|
||||||
|
return "", false
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemoteIP(r *http.Request) string {
|
||||||
|
ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr))
|
||||||
|
if err != nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return ip
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user