|
1 | 1 | package api |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "SamWaf/common/zlog" |
4 | 5 | "SamWaf/enums" |
5 | 6 | "SamWaf/global" |
6 | 7 | "SamWaf/model" |
7 | 8 | "SamWaf/model/common/response" |
8 | 9 | "SamWaf/model/request" |
9 | 10 | "SamWaf/model/spec" |
10 | 11 | "SamWaf/utils" |
| 12 | + "encoding/json" |
11 | 13 | "errors" |
| 14 | + "fmt" |
| 15 | + "io/ioutil" |
| 16 | + "net/http" |
| 17 | + "net/url" |
12 | 18 | "strings" |
| 19 | + "time" |
13 | 20 |
|
14 | 21 | "github.com/gin-gonic/gin" |
15 | 22 | "gorm.io/gorm" |
@@ -43,6 +50,21 @@ func (w *WafSslOrderApi) AddApi(c *gin.Context) { |
43 | 50 | response.FailWithMessage("IP证书只支持HTTP文件验证方式(http01),不支持DNS验证方式", c) |
44 | 51 | return |
45 | 52 | } |
| 53 | + if req.ApplyPlatform == "zerossl" { |
| 54 | + |
| 55 | + if global.GCONFIG_ZEROSSL_EAB_KID == "" || global.GCONFIG_ZEROSSL_EAB_HMAC_KEY == "" { |
| 56 | + if global.GCONFIG_ZEROSSL_ACCESS_KEY == "" { |
| 57 | + response.FailWithMessage("请配置zerossl访问key,在系统配置中 zerossl_access_key 中配置", c) |
| 58 | + return |
| 59 | + } |
| 60 | + // 调用 ZeroSSL API 获取 EAB 凭证 |
| 61 | + err := w.fetchAndUpdateZeroSSLEABCredentials() |
| 62 | + if err != nil { |
| 63 | + response.FailWithMessage(fmt.Sprintf("获取ZeroSSL EAB凭证失败: %s", err.Error()), c) |
| 64 | + return |
| 65 | + } |
| 66 | + } |
| 67 | + } |
46 | 68 | addResult, err := wafSslOrderService.AddApi(req) |
47 | 69 | if err == nil { |
48 | 70 | w.NotifyWaf(enums.ChanSslOrderSubmitted, addResult) |
@@ -171,3 +193,125 @@ func (w *WafSslOrderApi) check80Port(hosts model.Hosts) bool { |
171 | 193 | } |
172 | 194 | return false |
173 | 195 | } |
| 196 | + |
| 197 | +// fetchAndUpdateZeroSSLEABCredentials 调用 ZeroSSL API 获取 EAB 凭证并更新配置 |
| 198 | +func (w *WafSslOrderApi) fetchAndUpdateZeroSSLEABCredentials() error { |
| 199 | + // 构建请求 URL |
| 200 | + apiURL := "https://api.zerossl.com/acme/eab-credentials" |
| 201 | + u, err := url.Parse(apiURL) |
| 202 | + if err != nil { |
| 203 | + return fmt.Errorf("解析URL失败: %w", err) |
| 204 | + } |
| 205 | + |
| 206 | + // 添加查询参数(GET 请求) |
| 207 | + q := u.Query() |
| 208 | + q.Set("access_key", global.GCONFIG_ZEROSSL_ACCESS_KEY) |
| 209 | + u.RawQuery = q.Encode() |
| 210 | + |
| 211 | + // 创建 HTTP 客户端 |
| 212 | + client := &http.Client{ |
| 213 | + Timeout: 30 * time.Second, |
| 214 | + Transport: &http.Transport{ |
| 215 | + TLSHandshakeTimeout: 10 * time.Second, |
| 216 | + ResponseHeaderTimeout: 10 * time.Second, |
| 217 | + }, |
| 218 | + } |
| 219 | + |
| 220 | + // 发送 POST 请求(URL 中包含查询参数) |
| 221 | + zlog.Info("调用 ZeroSSL API 获取 EAB 凭证", "url", u.String()) |
| 222 | + req, err := http.NewRequest("POST", u.String(), nil) |
| 223 | + if err != nil { |
| 224 | + return fmt.Errorf("创建请求失败: %w", err) |
| 225 | + } |
| 226 | + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 227 | + |
| 228 | + resp, err := client.Do(req) |
| 229 | + if err != nil { |
| 230 | + return fmt.Errorf("HTTP请求失败: %w", err) |
| 231 | + } |
| 232 | + defer resp.Body.Close() |
| 233 | + |
| 234 | + // 读取响应体 |
| 235 | + body, err := ioutil.ReadAll(resp.Body) |
| 236 | + if err != nil { |
| 237 | + return fmt.Errorf("读取响应失败: %w", err) |
| 238 | + } |
| 239 | + |
| 240 | + // 检查 HTTP 状态码 |
| 241 | + if resp.StatusCode != http.StatusOK { |
| 242 | + return fmt.Errorf("API请求失败,状态码: %d, 响应: %s", resp.StatusCode, string(body)) |
| 243 | + } |
| 244 | + |
| 245 | + // 解析 JSON 响应 |
| 246 | + var apiResponse struct { |
| 247 | + Success bool `json:"success"` |
| 248 | + EabKid string `json:"eab_kid"` |
| 249 | + EabHmacKey string `json:"eab_hmac_key"` |
| 250 | + Error *struct { |
| 251 | + Code int `json:"code"` |
| 252 | + Type string `json:"type"` |
| 253 | + Message string `json:"message"` |
| 254 | + } `json:"error"` |
| 255 | + } |
| 256 | + |
| 257 | + if err := json.Unmarshal(body, &apiResponse); err != nil { |
| 258 | + return fmt.Errorf("解析JSON响应失败: %w, 原始响应: %s", err, string(body)) |
| 259 | + } |
| 260 | + |
| 261 | + // 检查 API 响应是否成功 |
| 262 | + if !apiResponse.Success { |
| 263 | + errorMsg := string(body) |
| 264 | + if apiResponse.Error != nil { |
| 265 | + errorMsg = fmt.Sprintf("错误代码: %d, 类型: %s, 消息: %s", |
| 266 | + apiResponse.Error.Code, apiResponse.Error.Type, apiResponse.Error.Message) |
| 267 | + } |
| 268 | + return fmt.Errorf("API返回失败: %s", errorMsg) |
| 269 | + } |
| 270 | + |
| 271 | + // 验证返回的凭证是否有效 |
| 272 | + if apiResponse.EabKid == "" || apiResponse.EabHmacKey == "" { |
| 273 | + return fmt.Errorf("API返回的凭证为空, 响应: %s", string(body)) |
| 274 | + } |
| 275 | + |
| 276 | + // 更新全局变量 |
| 277 | + global.GCONFIG_ZEROSSL_EAB_KID = apiResponse.EabKid |
| 278 | + global.GCONFIG_ZEROSSL_EAB_HMAC_KEY = apiResponse.EabHmacKey |
| 279 | + |
| 280 | + // 更新数据库配置 |
| 281 | + // 更新 zerossl_eab_kid |
| 282 | + eabKidConfig := wafSystemConfigService.GetDetailByItemApi(request.WafSystemConfigDetailByItemReq{Item: "zerossl_eab_kid"}) |
| 283 | + if eabKidConfig.Id != "" { |
| 284 | + err = wafSystemConfigService.ModifyApi(request.WafSystemConfigEditReq{ |
| 285 | + Id: eabKidConfig.Id, |
| 286 | + Item: eabKidConfig.Item, |
| 287 | + ItemClass: eabKidConfig.ItemClass, |
| 288 | + Value: apiResponse.EabKid, |
| 289 | + Remarks: eabKidConfig.Remarks, |
| 290 | + ItemType: eabKidConfig.ItemType, |
| 291 | + Options: eabKidConfig.Options, |
| 292 | + }) |
| 293 | + if err != nil { |
| 294 | + zlog.Warn("更新 zerossl_eab_kid 配置失败", "error", err.Error()) |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + // 更新 zerossl_eab_hmac_key |
| 299 | + eabHmacKeyConfig := wafSystemConfigService.GetDetailByItemApi(request.WafSystemConfigDetailByItemReq{Item: "zerossl_eab_hmac_key"}) |
| 300 | + if eabHmacKeyConfig.Id != "" { |
| 301 | + err = wafSystemConfigService.ModifyApi(request.WafSystemConfigEditReq{ |
| 302 | + Id: eabHmacKeyConfig.Id, |
| 303 | + Item: eabHmacKeyConfig.Item, |
| 304 | + ItemClass: eabHmacKeyConfig.ItemClass, |
| 305 | + Value: apiResponse.EabHmacKey, |
| 306 | + Remarks: eabHmacKeyConfig.Remarks, |
| 307 | + ItemType: eabHmacKeyConfig.ItemType, |
| 308 | + Options: eabHmacKeyConfig.Options, |
| 309 | + }) |
| 310 | + if err != nil { |
| 311 | + zlog.Warn("更新 zerossl_eab_hmac_key 配置失败", "error", err.Error()) |
| 312 | + } |
| 313 | + } |
| 314 | + |
| 315 | + zlog.Info("ZeroSSL EAB 凭证获取并更新成功") |
| 316 | + return nil |
| 317 | +} |
0 commit comments