@@ -14,16 +14,16 @@ import (
1414)
1515
1616// WebRoot 首页
17- func WebRoot (c * gin. Context ) {
17+ func WebRoot (ctx * Context ) {
1818// 301重定向
19- //c .Redirect(http.StatusMovedPermanently, "/static")
19+ //ctx.C .Redirect(http.StatusMovedPermanently, "/static")
2020// 返回HTML页面
21- //c .HTML(http.StatusOK, "index.html", nil)
22- c .HTML (http .StatusOK , "index.html" , gin.H {})
21+ //ctx.C .HTML(http.StatusOK, "index.html", nil)
22+ ctx . C .HTML (http .StatusOK , "index.html" , gin.H {})
2323}
2424
2525// SystemInfo 获取系统信息
26- func SystemInfo (c * gin. Context ) {
26+ func SystemInfo (ctx * Context ) {
2727data := make (map [string ]interface {}, 0 )
2828data ["Version" ] = utils .ToUpper (runtime .Version ())
2929data ["cpu" ] = runtime .NumCPU ()
@@ -41,28 +41,39 @@ func SystemInfo(c *gin.Context) {
4141// 获取当前存在的go协程数
4242data ["NumGoroutine" ] = runtime .NumGoroutine ()
4343
44- SuccessJSON (c , "获取系统信息成功" , data )
44+ ctx .SuccessJSON ("获取系统信息成功" , data )
45+ }
46+
47+ // GetKeyInfo 定义了GetKey的 JSON 结构体
48+ type GetKeyInfo struct {
49+ Company string `json:"company" binding:"required"`
50+ App string `json:"app" binding:"required"`
51+ Version string `json:"version" binding:"required"`
4552}
4653
4754// GetKey 获取key
48- func GetKey (c * gin. Context ) {
55+ func GetKey (ctx * Context ) {
4956// GET 获取参数内容,没有则返回空字符串
50- //company := c .Query("company")
57+ //company := ctx.C .Query("company")
5158// POST 获取的所有参数内容的类型都是 string
52- company := c .PostForm ("company" )
59+ company := ctx . C .PostForm ("company" )
5360
61+ /*var getKeyInfo GetKeyInfo
62+ if !ctx.C.BindAndValidate(&getKeyInfo) {
63+ return
64+ }*/
5465if utils .IsStringEmpty (company ) {
55- ErrorJSON (c , 300 , "请选择公司" )
66+ ctx . ErrorJSON (300 , "请选择公司" )
5667return
5768}
58- app := c .PostForm ("app" )
69+ app := ctx . C .PostForm ("app" )
5970if utils .IsStringEmpty (app ) {
60- ErrorJSON (c , 300 , "请选择产品" )
71+ ctx . ErrorJSON (300 , "请选择产品" )
6172return
6273}
63- version := c .PostForm ("version" )
74+ version := ctx . C .PostForm ("version" )
6475if utils .IsStringEmpty (version ) {
65- ErrorJSON (c , 300 , "请选择版本" )
76+ ctx . ErrorJSON (300 , "请选择版本" )
6677return
6778}
6879dir := TempDirPath + string (filepath .Separator )
@@ -85,38 +96,38 @@ func GetKey(c *gin.Context) {
8596ExecuteScriptError (err )
8697if err != nil {
8798log .Println (err )
88- ErrorJSON (c , http .StatusInternalServerError , "系统错误!" )
99+ ctx . ErrorJSON (http .StatusInternalServerError , "系统错误!" )
89100return
90101}
91- SuccessJSON (c , "获取key成功" , map [string ]string {"key" : out })
102+ ctx . SuccessJSON ("获取key成功" , map [string ]string {"key" : out })
92103
93104} else if company == "mobatek" {
94105curr , err := utils .OsPath ()
95106if err != nil {
96- SystemErrorJSON (c , http . StatusInternalServerError , "系统错误!" )
107+ ctx . SystemErrorJSON (ERROR , "系统错误!" )
97108return
98109}
99110_ , err = utils .ExecutePython (dir + "moba_xterm_Keygen.py" , curr , version )
100111ExecuteScriptError (err )
101112if err != nil {
102- SystemErrorJSON (c , http . StatusInternalServerError , "系统错误!" )
113+ ctx . SystemErrorJSON (ERROR , "系统错误!" )
103114return
104115}
105- c .Header ("Content-Type" , "application/octet-stream" )
106- c .Header ("Content-Disposition" , "attachment; filename=\" Custom.mxtpro\" " )
107- //c .Writer.Header().Set("Content-Type", "application/octet-stream")
108- //c .Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", "Custom.mxtpro"))
116+ ctx . C .Header ("Content-Type" , "application/octet-stream" )
117+ ctx . C .Header ("Content-Disposition" , "attachment; filename=\" Custom.mxtpro\" " )
118+ //ctx.C .Writer.Header().Set("Content-Type", "application/octet-stream")
119+ //ctx.C .Writer.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", "Custom.mxtpro"))
109120
110- c .FileAttachment (filepath .Join (curr , "Custom.mxtpro" ), "Custom.mxtpro" )
121+ ctx . C .FileAttachment (filepath .Join (curr , "Custom.mxtpro" ), "Custom.mxtpro" )
111122
112123} else if company == "torchsoft" {
113124out , err := utils .ExecutePython (dir + "reg_workshop_keygen.py" , version )
114125ExecuteScriptError (err )
115126if err != nil {
116- ErrorJSON (c , http .StatusInternalServerError , "系统错误!" )
127+ ctx . ErrorJSON (http .StatusInternalServerError , "系统错误!" )
117128return
118129}
119- SuccessJSON (c , "获取key成功" , map [string ]string {"key" : out })
130+ ctx . SuccessJSON ("获取key成功" , map [string ]string {"key" : out })
120131}
121132}
122133
@@ -133,9 +144,9 @@ func ExecuteScriptError(err error) {
133144}
134145
135146// Upload 文件上传请求
136- func Upload (c * gin. Context ) {
147+ func Upload (ctx * Context ) {
137148// 拿到上传的文件的信息
138- file , header , err := c .Request .FormFile ("upload" )
149+ file , header , err := ctx . C .Request .FormFile ("upload" )
139150filename := header .Filename
140151log .Println (header .Filename )
141152out , err := os .Create ("./tmp/" + filename + ".png" )
@@ -156,81 +167,81 @@ func Upload(c *gin.Context) {
156167}
157168
158169// Download 文件下载请求
159- func Download (c * gin. Context ) {
160- response , err := http .Get (c .Request .Host + "/static/public/favicon.ico" )
170+ func Download (ctx * Context ) {
171+ response , err := http .Get (ctx . C .Request .Host + "/static/public/favicon.ico" )
161172if err != nil || response .StatusCode != http .StatusOK {
162- c .Status (http .StatusServiceUnavailable )
173+ ctx . C .Status (http .StatusServiceUnavailable )
163174return
164175}
165176
166177extraHeaders := map [string ]string {
167178"Content-Disposition" : `attachment; filename="favicon.ico"` ,
168179}
169180
170- c .DataFromReader (http .StatusOK , response .ContentLength , response .Header .Get ("Content-Type" ), response .Body , extraHeaders )
181+ ctx . C .DataFromReader (http .StatusOK , response .ContentLength , response .Header .Get ("Content-Type" ), response .Body , extraHeaders )
171182}
172183
173184// GetNetSarangDownloadUrl 获取NetSarang下载url
174- func GetNetSarangDownloadUrl (c * gin. Context ) {
185+ func GetNetSarangDownloadUrl (ctx * Context ) {
175186// POST 获取的所有参数内容的类型都是 string
176- app := c .PostForm ("app" )
187+ app := ctx . C .PostForm ("app" )
177188if utils .IsStringEmpty (app ) {
178- ErrorJSON (c , 300 , "请选择产品" )
189+ ctx . ErrorJSON (300 , "请选择产品" )
179190return
180191}
181- version := c .PostForm ("version" )
192+ version := ctx . C .PostForm ("version" )
182193if utils .IsStringEmpty (version ) {
183- ErrorJSON (c , 300 , "请选择版本" )
194+ ctx . ErrorJSON (300 , "请选择版本" )
184195return
185196}
186197url , err := reptile .NetsarangGetInfo (app )
187198if err != nil {
188- ErrorJSON (c , http .StatusInternalServerError , "系统错误!" )
199+ ctx . ErrorJSON (http .StatusInternalServerError , "系统错误!" )
189200return
190201}
191- SuccessJSON (c , "获取" + app + "成功" , map [string ]string {"url" : url })
202+ ctx . SuccessJSON ("获取" + app + "成功" , map [string ]string {"url" : url })
192203}
193204
194205// NginxFormatIndex NGINX格式化代码页面
195- func NginxFormatIndex (c * gin. Context ) {
206+ func NginxFormatIndex (ctx * Context ) {
196207// 301重定向
197- //c .Redirect(http.StatusMovedPermanently, "/static")
208+ //ctx.C .Redirect(http.StatusMovedPermanently, "/static")
198209// 返回HTML页面
199- //c .HTML(http.StatusOK, "index.html", nil)
200- c .HTML (http .StatusOK , "nginx-format.html" , gin.H {})
210+ //ctx.C .HTML(http.StatusOK, "index.html", nil)
211+ ctx . C .HTML (http .StatusOK , "nginx-format.html" , gin.H {})
201212}
202213
203214// NginxFormatPython 格式化nginx配置代码
204- func NginxFormatPython (c * gin. Context ) {
215+ func NginxFormatPython (ctx * Context ) {
205216// GET 获取参数内容,没有则返回空字符串
206- //code := c .Query("code")
217+ //code := ctx.C .Query("code")
207218// POST 获取的所有参数内容的类型都是 string
208- code := c .PostForm ("code" )
219+ code := ctx . C .PostForm ("code" )
209220
210221if utils .IsStringEmpty (code ) {
211- ErrorJSON (c , 300 , "请输入配置代码" )
222+ ctx . ErrorJSON (300 , "请输入配置代码" )
212223return
213224}
214225out , err := utils .ExecutePython (TempDirPath + string (filepath .Separator )+ "nginxfmt.py" , code )
215226if err != nil {
216227log .Println (err )
217- ErrorJSON (c , http .StatusInternalServerError , "系统错误!" )
228+ ctx . ErrorJSON (http .StatusInternalServerError , "系统错误!" )
218229return
219230}
220231res := make (map [string ]string )
221232res ["contents" ] = out
222- SuccessJSON (c , "请求成功" , res )
233+ ctx . SuccessJSON ("请求成功" , res )
223234}
224235
225236// GetNavicatDownloadUrl 获取navicat下载地址
226- func GetNavicatDownloadUrl (c * gin. Context ) {
227- location , isExist := c .GetQuery ("location" )
237+ func GetNavicatDownloadUrl (ctx * Context ) {
238+ location , isExist := ctx . C .GetQuery ("location" )
228239if location == "" || ! isExist {
229- location = c .DefaultPostForm ("location" , "1" )
240+ location = ctx . C .DefaultPostForm ("location" , "1" )
230241}
231- product , isExist := c .GetQuery ("product" )
242+ product , isExist := ctx . C .GetQuery ("product" )
232243if product == "" || ! isExist {
233- product = c .DefaultPostForm ("product" , "navicat_premium_cs_x64.exe" )
244+ product = ctx . C .DefaultPostForm ("product" , "navicat_premium_cs_x64.exe" )
234245}
235246
236247// POST 获取的所有参数内容的类型都是 string
@@ -244,19 +255,38 @@ func GetNavicatDownloadUrl(c *gin.Context) {
244255result , err := utils .HttpReadBodyJsonMap (http .MethodPost , url , utils .ContentTypeAXWFU , params , nil )
245256
246257if result == nil || err != nil {
247- ErrorJSON (c , http .StatusInternalServerError , "系统错误!" )
258+ ctx . ErrorJSON (http .StatusInternalServerError , "系统错误!" )
248259return
249260}
250- SuccessJSON (c , "获取下载地址成功" , map [string ]string {"url" : result ["download_link" ].(string )})
261+ ctx . SuccessJSON ("获取下载地址成功" , map [string ]string {"url" : result ["download_link" ].(string )})
251262}
252263
253- func GetSvp (c * gin. Context ) {
264+ func GetSvp (ctx * Context ) {
254265defer func () { // 捕获panic
255266if r := recover (); r != nil {
256267log .Println ("Recovered from panic:" , r )
257- c .String (http .StatusOK , r .(string ))
268+ ctx . C .String (http .StatusOK , r .(string ))
258269}
259270}()
260- log .Println ("GetSvp Header:" , c .Request .Header )
261- c .String (http .StatusOK , reptile .GetSvpAll ())
271+ //log.Println("GetSvp Header:", ctx.C.Request.Header)
272+ ctx .C .String (http .StatusOK , reptile .GetSvpAllHandler (getClientIP (ctx .C .Request )))
273+ }
274+
275+ // getClientIP 尝试从请求中获取真实的客户端IP
276+ func getClientIP (r * http.Request ) string {
277+ // 检查 X-Forwarded-For 头,这是代理服务器常用的方式
278+ ip := r .Header .Get ("X-Forwarded-For" )
279+ if ip != "" {
280+ // X-Forwarded-For 可能包含多个IP,取第一个
281+ return strings .Split (ip , "," )[0 ]
282+ }
283+ // 检查 X-Real-IP 头
284+ ip = r .Header .Get ("X-Real-IP" )
285+ if ip != "" {
286+ return ip
287+ }
288+ // 如果都没有,则使用 RemoteAddr
289+ // RemoteAddr 格式为 "IP:port",我们需要去掉端口
290+ ip , _ , _ = strings .Cut (r .RemoteAddr , ":" )
291+ return ip
262292}
0 commit comments