|
| 1 | +package report |
| 2 | + |
| 3 | +import ( |
| 4 | + "ALLinSSL/backend/public" |
| 5 | + "context" |
| 6 | + "crypto/tls" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "github.com/go-resty/resty/v2" |
| 10 | + "net/http" |
| 11 | + "strings" |
| 12 | + "time" |
| 13 | +) |
| 14 | + |
| 15 | +type ReportConfig struct { |
| 16 | + Url string `json:"url"` |
| 17 | + Data string `json:"data,omitempty"` |
| 18 | + Method string `json:"method,omitempty"` |
| 19 | + Headers string `json:"headers,omitempty"` |
| 20 | + IgnoreSSL bool `json:"ignore_ssl,omitempty"` |
| 21 | +} |
| 22 | + |
| 23 | +type WebHookReporter struct { |
| 24 | + config *ReportConfig |
| 25 | + logger *public.Logger |
| 26 | + httpClient *resty.Client |
| 27 | +} |
| 28 | + |
| 29 | +func NewWebHookReporter(config *ReportConfig, logger *public.Logger) *WebHookReporter { |
| 30 | + client := resty.New() |
| 31 | + client.SetTimeout(30 * time.Second) |
| 32 | + |
| 33 | + if config.IgnoreSSL { |
| 34 | + client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) |
| 35 | + } |
| 36 | + |
| 37 | + return &WebHookReporter{ |
| 38 | + config: config, |
| 39 | + logger: logger, |
| 40 | + httpClient: client, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func (w *WebHookReporter) Send(ctx context.Context) error { |
| 45 | + // 确定HTTP方法 |
| 46 | + method := strings.ToUpper(w.config.Method) |
| 47 | + if method == "" { |
| 48 | + method = http.MethodPost // 默认使用POST方法 |
| 49 | + } |
| 50 | + |
| 51 | + // 创建基础请求 |
| 52 | + req := w.httpClient.R(). |
| 53 | + SetContext(ctx) |
| 54 | + |
| 55 | + // 设置请求头 |
| 56 | + if w.config.Headers != "" { |
| 57 | + reqHeader, err := w.ParseHeaders(w.config.Headers) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("解析请求头错误: %w", err) |
| 60 | + } |
| 61 | + req.Header = reqHeader |
| 62 | + } |
| 63 | + |
| 64 | + switch method { |
| 65 | + case http.MethodPost: |
| 66 | + { |
| 67 | + contentType := req.Header.Get("application/json") |
| 68 | + if contentType == "" { |
| 69 | + contentType = "application/json" |
| 70 | + } |
| 71 | + switch contentType { |
| 72 | + case "application/json": |
| 73 | + req.SetHeader("Content-Type", "application/json") |
| 74 | + var reqData interface{} |
| 75 | + err := json.Unmarshal([]byte(w.config.Data), &reqData) |
| 76 | + if err != nil { |
| 77 | + return fmt.Errorf("webhook数据解析失败err: %w", err) |
| 78 | + } |
| 79 | + req.SetBody(reqData) |
| 80 | + case "application/x-www-form-urlencoded": |
| 81 | + req.SetHeader("Content-Type", "application/x-www-form-urlencoded") |
| 82 | + reqData := make(map[string]string) |
| 83 | + err := json.Unmarshal([]byte(w.config.Data), &reqData) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("webhook数据解析失败err: %w", err) |
| 86 | + } |
| 87 | + req.SetFormData(reqData) |
| 88 | + case "multipart/form-data": |
| 89 | + req.SetHeader("Content-Type", "multipart/form-data") |
| 90 | + reqData := make(map[string]string) |
| 91 | + err := json.Unmarshal([]byte(w.config.Data), &reqData) |
| 92 | + if err != nil { |
| 93 | + return fmt.Errorf("webhook数据解析失败err: %w", err) |
| 94 | + } |
| 95 | + req.SetMultipartFormData(reqData) |
| 96 | + } |
| 97 | + } |
| 98 | + case http.MethodGet: |
| 99 | + { |
| 100 | + reqData := make(map[string]string) |
| 101 | + err := json.Unmarshal([]byte(w.config.Data), &reqData) |
| 102 | + if err != nil { |
| 103 | + return fmt.Errorf("webhook数据解析失败err: %w", err) |
| 104 | + } |
| 105 | + req.SetQueryParams(reqData) |
| 106 | + } |
| 107 | + default: |
| 108 | + return fmt.Errorf("暂不支持的HTTP方法: %s", method) |
| 109 | + } |
| 110 | + |
| 111 | + // 发送请求 |
| 112 | + resp, err := req.Execute(method, w.config.Url) |
| 113 | + if err != nil { |
| 114 | + if w.logger != nil { |
| 115 | + w.logger.Error(fmt.Sprintf("Webhook请求失败%s %v", w.config.Url, err)) |
| 116 | + } |
| 117 | + |
| 118 | + return fmt.Errorf("webhook请求失败: %w", err) |
| 119 | + } |
| 120 | + |
| 121 | + // 处理响应 |
| 122 | + if resp.IsError() { |
| 123 | + if w.logger != nil { |
| 124 | + w.logger.Error(fmt.Sprintf("Webhook返回错误响应%s %d", w.config.Url, resp.StatusCode())) |
| 125 | + } |
| 126 | + return fmt.Errorf("webhook返回错误状态码: %d", resp.StatusCode()) |
| 127 | + } |
| 128 | + |
| 129 | + if w.logger != nil { |
| 130 | + w.logger.Debug(fmt.Sprintf("Webhook请求成功 %s", w.config.Url)) |
| 131 | + } |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +func (w *WebHookReporter) ParseHeaders(headerStr string) (http.Header, error) { |
| 136 | + headers := make(http.Header) |
| 137 | + lines := strings.Split(headerStr, "\n") |
| 138 | + |
| 139 | + for i, line := range lines { |
| 140 | + line = strings.TrimSpace(line) |
| 141 | + if line == "" { |
| 142 | + continue |
| 143 | + } |
| 144 | + parts := strings.SplitN(line, ":", 2) |
| 145 | + if len(parts) != 2 { |
| 146 | + return nil, fmt.Errorf("解析请求头错误 第%d行: %s", i+1, line) |
| 147 | + } |
| 148 | + key := strings.TrimSpace(parts[0]) |
| 149 | + value := strings.TrimSpace(parts[1]) |
| 150 | + if key == "" || value == "" { |
| 151 | + return nil, fmt.Errorf("请求头Key第%d行为空", i+1) |
| 152 | + } |
| 153 | + canonicalKey := http.CanonicalHeaderKey(key) |
| 154 | + headers.Add(canonicalKey, value) |
| 155 | + } |
| 156 | + |
| 157 | + return headers, nil |
| 158 | +} |
| 159 | + |
| 160 | +func NotifyWebHook(params map[string]any) error { |
| 161 | + if params == nil { |
| 162 | + return fmt.Errorf("缺少参数") |
| 163 | + } |
| 164 | + providerID := params["provider_id"].(string) |
| 165 | + |
| 166 | + var logger *public.Logger |
| 167 | + |
| 168 | + if params["logger"] != nil { |
| 169 | + logger = params["logger"].(*public.Logger) |
| 170 | + } |
| 171 | + |
| 172 | + providerData, err := GetReport(providerID) |
| 173 | + if err != nil { |
| 174 | + return err |
| 175 | + } |
| 176 | + configStr := providerData["config"].(string) |
| 177 | + var config ReportConfig |
| 178 | + err = json.Unmarshal([]byte(configStr), &config) |
| 179 | + if err != nil { |
| 180 | + return fmt.Errorf("解析配置失败: %v", err) |
| 181 | + } |
| 182 | + |
| 183 | + reporter := NewWebHookReporter(&config, logger) |
| 184 | + httpctx := context.Background() |
| 185 | + err = reporter.Send(httpctx) |
| 186 | + if err != nil { |
| 187 | + return fmt.Errorf("webhook发送失败: %w", err) |
| 188 | + } |
| 189 | + return nil |
| 190 | +} |
0 commit comments