This commit is contained in:
aixiao 2024-07-19 15:05:40 +08:00
commit fd522fdab7
7 changed files with 143 additions and 0 deletions

BIN
amd64 Normal file

Binary file not shown.

17
build.sh Normal file
View File

@ -0,0 +1,17 @@
:
_build()
{
ARCH="amd64"
for a in $ARCH
do
CGO_ENABLED=0 GOOS=linux GOARCH=$a go build -ldflags '-w -s' -o $a main.go && upx -9 $a
done
cp amd64 inet-ip
}
_build

10
go.mod Normal file
View File

@ -0,0 +1,10 @@
module main
go 1.21.1
require github.com/sevlyar/go-daemon v0.1.6
require (
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
golang.org/x/sys v0.22.0 // indirect
)

6
go.sum Normal file
View File

@ -0,0 +1,6 @@
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 h1:iQTw/8FWTuc7uiaSepXwyf3o52HaUYcV+Tu66S3F5GA=
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0/go.mod h1:1NbS8ALrpOvjt0rHPNLyCIeMtbizbir8U//inJ+zuB8=
github.com/sevlyar/go-daemon v0.1.6 h1:EUh1MDjEM4BI109Jign0EaknA2izkOyi0LV3ro3QQGs=
github.com/sevlyar/go-daemon v0.1.6/go.mod h1:6dJpPatBT9eUwM5VCw9Bt6CdX9Tk6UWvhW3MebLDRKE=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

BIN
inet-ip Normal file

Binary file not shown.

17
inet-ip.conf Normal file
View File

@ -0,0 +1,17 @@
server {
listen 80;
listen 443 ssl;
server_name inet-ip.aixiao.me;
ssl_certificate /opt/nginx/cert/aixiao.me.cer;
ssl_certificate_key /opt/nginx/cert/aixiao.me.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
location / {
proxy_pass http://localhost:90;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

93
main.go Normal file
View File

@ -0,0 +1,93 @@
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
"github.com/sevlyar/go-daemon"
)
// 获取客户端IP的函数
func getClientIP(r *http.Request) string {
// 尝试从X-Forwarded-For头部获取IP
forwarded := r.Header.Get("X-Forwarded-For")
if forwarded != "" {
// X-Forwarded-For可能包含多个IP地址取第一个
ips := strings.Split(forwarded, ",")
return strings.TrimSpace(ips[0])
}
// 尝试从X-Real-Ip头部获取IP
realIP := r.Header.Get("X-Real-Ip")
if realIP != "" {
return realIP
}
// 最后从RemoteAddr中获取IP
return r.RemoteAddr
}
// 处理HTTP请求的函数
func handler(w http.ResponseWriter, r *http.Request) {
clientIP := getClientIP(r)
response := fmt.Sprintf("%s", clientIP)
fmt.Fprintln(w, response)
log.Printf("Handled request from %s\n", clientIP) // 记录日志
}
func main() {
// 定义守护进程的启动配置
cntxt := &daemon.Context{
PidFileName: "inet-ip.pid",
PidFilePerm: 0644,
LogFileName: "inet-ip.log",
LogFilePerm: 0640,
WorkDir: "./",
Umask: 027,
Args: []string{"[inet-ip]"},
}
// 启动守护进程
d, err := cntxt.Reborn()
if err != nil {
log.Fatal("Unable to run: ", err)
}
if d != nil {
return
}
defer cntxt.Release()
// 设置日志输出到文件
logFile, err := os.OpenFile("inet-ip.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatalf("Error opening log file: %s", err)
}
defer logFile.Close()
log.SetOutput(logFile)
// 启动HTTP服务器
go serveHTTP()
// 创建一个通道来接收操作系统信号
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
log.Println("Server started")
// 等待信号
<-stop
// 服务器停止时记录日志
log.Println("Server stopped")
}
func serveHTTP() {
http.HandleFunc("/", handler)
fmt.Println("Starting server on :90")
if err := http.ListenAndServe(":90", nil); err != nil && err != http.ErrServerClosed {
log.Fatalf("Error starting server: %s", err)
}
}