Skip to content
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
2 changes: 1 addition & 1 deletion .github/workflows/build-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
go-version: ${{ env.GO_VERSION }}
- name: Build
run: |
CGO_ENABLED=0 go build -o ${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}} ./main.go
CGO_ENABLED=0 go build -o ${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}} .
- name: Package
run: |
tar -czvf ${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}}.tar.gz ./${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
go-version: ${{ env.GO_VERSION }}
- name: Build
run: |
CGO_ENABLED=0 go build -ldflags "-s -w" -o ${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}} ./main.go
CGO_ENABLED=0 go build -ldflags "-s -w" -o ${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}} .
- name: Package
run: |
tar -czvf ${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}}.tar.gz ./${{ env.OUTPUT_BINARY }}-${{matrix.goos}}-${{matrix.goarch}}
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 更新日志

3.0.0-beta.0
---
- 使用Touka框架重构, 去除caddy依赖, 大幅减小体积和额外的开销

v2.1.4
---
- UPDATE: 安全性更新
Expand Down
2 changes: 1 addition & 1 deletion DEV-VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
25w03b
3.0.0-beta.0
18 changes: 9 additions & 9 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
package api

import (
"ip/bilibili"
"ip/bapi"
"ip/config"
"ip/ip"

"github.com/gin-gonic/gin"
"github.com/infinite-iroha/touka"
)

func InitHandleRouter(cfg *config.Config, router *gin.Engine) {
func InitHandleRouter(cfg *config.Config, router *touka.Engine) {
apiRouter := router.Group("api")
{
apiRouter.GET("/healthcheck", func(c *gin.Context) {
c.JSON(200, gin.H{
apiRouter.GET("/healthcheck", func(c *touka.Context) {
c.JSON(200, touka.H{
"status": "ok",
})
})
apiRouter.Any("/ip-lookup", func(c *gin.Context) {
apiRouter.GET("/ip-lookup", func(c *touka.Context) {
ip.IPHandler(c)
})
apiRouter.Any("/ip", func(c *gin.Context) {
apiRouter.GET("/ip", func(c *touka.Context) {
ip.IPPureHandler(c)
})
apiRouter.Any("/bilibili", func(c *gin.Context) {
bilibili.Bilibili(c)
apiRouter.GET("/bilibili", func(c *touka.Context) {
bapi.Bilibili(c)
})
}
}
71 changes: 71 additions & 0 deletions bapi/bapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package bapi

import (
"net/http"
"net/netip"
"net/url"

"github.com/infinite-iroha/touka"
)

// 使用req库处理 /bilibili 路由的请求并使用chrome的TLS指纹
func Bilibili(c *touka.Context) {

// 设置响应头
c.Header("Access-Control-Allow-Origin", "*") // 允许跨域请求
c.Header("Content-Type", "application/json") // 内容类型

// 从请求中获取ip参数
ip := c.Query("ip")
if ip == "" {
c.JSON(http.StatusBadRequest, touka.H{
"error": "Missing ip parameter",
})
// IP METHOD URL UA PROTOCAL
c.Warnf("%s %s %s %s %s Missing ip parameter", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.Header.Get("User-Agent"), c.Request.Proto)
return
}

_, err := netip.ParseAddr(ip)
if err != nil {
c.JSON(http.StatusBadRequest, touka.H{
"error": "Invalid ip parameter",
})
// IP METHOD URL UA PROTOCAL
c.Warnf("%s %s %s %s %s Invalid ip parameter", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.Header.Get("User-Agent"), c.Request.Proto)
return
}

// 定义源API的URL并添加ip参数
apiURL := "https://api.live.bilibili.com/ip_service/v1/ip_service/get_ip_addr?ip=" + url.QueryEscape(ip)

// 使用req库发送请求并使用chrome的TLS指纹
client := c.GetHTTPC()
rb := client.NewRequestBuilder("GET", apiURL)
rb.NoDefaultHeaders()
rb.SetHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36")

body, err := rb.Bytes()
if err != nil {
c.JSON(http.StatusInternalServerError, touka.H{
"error": "Failed to get response from source API",
})
// IP METHOD URL UA PROTOCAL ERROR
c.Errorf("%s %s %s %s %s Failed to get response from source API: %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
return
}

if len(body) == 0 {
c.JSON(http.StatusInternalServerError, touka.H{
"error": "Failed to get response from source API",
})
// IP METHOD URL UA PROTOCAL ERROR
c.Errorf("%s %s %s %s %s Failed to get response from source API: %s", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.Header.Get("User-Agent"), c.Request.Proto, err)
return
}

// 返回上游API的响应内容
c.Raw(http.StatusOK, "application/json", body)
// IP METHOD URL UA PROTOCAL
c.Infof("%s %s %s %s %s Success", c.ClientIP(), c.Request.Method, c.Request.URL.Path, c.Request.Header.Get("User-Agent"), c.Request.Proto)
}
105 changes: 0 additions & 105 deletions bilibili/bilibili.go

This file was deleted.

66 changes: 66 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
"os"

"github.com/BurntSushi/toml"
)

Expand Down Expand Up @@ -30,9 +32,73 @@ type MmdbConfig struct {

// LoadConfig 从 TOML 配置文件加载配置
func LoadConfig(filePath string) (*Config, error) {
if !FileExists(filePath) {
// 楔入配置文件
err := DefaultConfig().WriteConfig(filePath)
if err != nil {
return nil, err
}
return DefaultConfig(), nil
}

var config Config
if _, err := toml.DecodeFile(filePath, &config); err != nil {
return nil, err
}
return &config, nil
}

// 写入配置文件
func (c *Config) WriteConfig(filePath string) error {
file, err := os.Create(filePath)
if err != nil {
return err
}
defer file.Close()

encoder := toml.NewEncoder(file)
return encoder.Encode(c)
}

// 检测文件是否存在
func FileExists(filename string) bool {
_, err := os.Stat(filename)
return !os.IsNotExist(err)
}

/*
[server]
host = "127.0.0.1"
port = 8080

[mmdb]
mmdbpath = "/data/ip/db"
asndbpath = "/data/ip/db/asn.mmdb"
countrydbpath = "/data/ip/db/country.mmdb"
ipinfoKey = ""
updateFreq = 24 # hours

[log]
logfilepath = "/data/ip/log/ip.log"
maxlogsize = 5 # MB
*/

func DefaultConfig() *Config {
return &Config{
Server: ServerConfig{
Host: "0.0.0.0",
Port: 8080,
},
Log: LogConfig{
LogFilePath: "./log/ip.log",
MaxLogSize: 5, // MB
},
Mmdb: MmdbConfig{
MmDBPath: "./data",
ASNDBPath: "./data/asn.mmdb",
CountryDBPath: "./data/country.mmdb",
IPinfoKey: "",
UpdateFreq: 24, // hours
},
}
}
Loading
Loading