| 116 | } |
| 117 | |
| 118 | func (api *API) WebCall() { |
| 119 | tlsConfig := &tls.Config{ |
| 120 | InsecureSkipVerify: true, |
| 121 | } |
| 122 | transport := &http.Transport{TLSClientConfig: tlsConfig} |
| 123 | // Support proxy connections for web requests |
| 124 | if api.Proxy != "" { |
| 125 | socks, err := proxy.SOCKS5("tcp", api.Proxy, nil, &net.Dialer{Timeout: 5 * time.Second}) |
| 126 | if err != nil { |
| 127 | api.Log.Errorf([]interface{}{api.Name}, "Failed to establish SOCKS connection %v", err) |
| 128 | return |
| 129 | } |
| 130 | transport.Dial = socks.Dial |
| 131 | } |
| 132 | |
| 133 | client := &http.Client{ |
| 134 | Transport: transport, |
| 135 | CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 136 | return http.ErrUseLastResponse |
| 137 | }, |
| 138 | } |
| 139 | |
| 140 | req, err := http.NewRequest(api.Method, api.URL, bytes.NewBuffer([]byte(api.Data))) |
| 141 | if err != nil { |
| 142 | api.Log.Errorf(nil, "Request Error (%s): %v", api.URL, err) |
| 143 | return |
| 144 | } |
| 145 | |
| 146 | if api.Opts != nil { |
| 147 | for k, v := range *api.Opts { |
| 148 | switch k { |
| 149 | case "Header": |
| 150 | req.Header = v.(map[string][]string) |
| 151 | case "CheckRedirect": |
| 152 | client.CheckRedirect = v.(func(req *http.Request, via []*http.Request) error) |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | if api.Debug > 0 { |
| 158 | api.Log.Debugf([]interface{}{api.Name}, "REQUEST HEADER: %s %s %s", req.URL, req.Proto, req.Header) |
| 159 | if api.Debug > 1 { |
| 160 | api.Log.Debugf([]interface{}{api.Name}, "REQUEST BODY: %s", req.Body) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | resp, err := client.Do(req) |
| 165 | if err != nil { |
| 166 | if api.Debug > 0 { |
| 167 | api.Log.Debugf([]interface{}{api.Name}, "Dial Error: %v", err) |
| 168 | } |
| 169 | return |
| 170 | } |
| 171 | |
| 172 | defer resp.Body.Close() |
| 173 | bodyBytes, err := io.ReadAll(resp.Body) |
| 174 | if err != nil { |
| 175 | api.Log.Errorf([]interface{}{api.Name}, "Unable to read response: %v", err) |