add handler recorder
This commit is contained in:
+27
-2
@@ -5,6 +5,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/recorder"
|
||||
@@ -12,6 +13,7 @@ import (
|
||||
|
||||
type httpRecorderOptions struct {
|
||||
timeout time.Duration
|
||||
header http.Header
|
||||
}
|
||||
|
||||
type HTTPRecorderOption func(opts *httpRecorderOptions)
|
||||
@@ -22,9 +24,16 @@ func TimeoutHTTPRecorderOption(timeout time.Duration) HTTPRecorderOption {
|
||||
}
|
||||
}
|
||||
|
||||
func HeaderHTTPRecorderOption(header http.Header) HTTPRecorderOption {
|
||||
return func(opts *httpRecorderOptions) {
|
||||
opts.header = header
|
||||
}
|
||||
}
|
||||
|
||||
type httpRecorder struct {
|
||||
url string
|
||||
httpClient *http.Client
|
||||
header http.Header
|
||||
}
|
||||
|
||||
// HTTPRecorder records data to HTTP service.
|
||||
@@ -34,11 +43,19 @@ func HTTPRecorder(url string, opts ...HTTPRecorderOption) recorder.Recorder {
|
||||
opt(&options)
|
||||
}
|
||||
|
||||
if url == "" {
|
||||
return nil
|
||||
}
|
||||
if !strings.HasPrefix(url, "http") {
|
||||
url = "http://" + url
|
||||
}
|
||||
|
||||
return &httpRecorder{
|
||||
url: url,
|
||||
httpClient: &http.Client{
|
||||
Timeout: options.timeout,
|
||||
},
|
||||
header: options.header,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,14 +65,22 @@ func (r *httpRecorder) Record(ctx context.Context, b []byte, opts ...recorder.Re
|
||||
return err
|
||||
}
|
||||
|
||||
if r.header != nil {
|
||||
req.Header = r.header
|
||||
}
|
||||
|
||||
if req.Header.Get("Content-Type") == "" {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
|
||||
resp, err := r.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("%d %s", resp.StatusCode, resp.Status)
|
||||
if resp.StatusCode >= http.StatusBadRequest {
|
||||
return fmt.Errorf(resp.Status)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -1,6 +1,68 @@
|
||||
package recorder
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/recorder"
|
||||
)
|
||||
|
||||
const (
|
||||
RecorderServiceHandler = "recorder.service.handler"
|
||||
RecorderServiceHandlerSerial = "recorder.service.handler.serial"
|
||||
RecorderServiceHandlerTunnel = "recorder.service.handler.tunnel"
|
||||
)
|
||||
|
||||
type HTTPRecorderObject struct {
|
||||
Host string `json:"host"`
|
||||
Method string `json:"method"`
|
||||
Proto string `json:"proto"`
|
||||
Scheme string `json:"scheme"`
|
||||
URI string `json:"uri"`
|
||||
StatusCode int `json:"statusCode"`
|
||||
RequestHeader http.Header `json:"requestHeader"`
|
||||
ResponseHeader http.Header `json:"responseHeader"`
|
||||
// RequestBody string
|
||||
// ResponseBody string
|
||||
}
|
||||
|
||||
type DNSRecorderObject struct {
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Class string `json:"class"`
|
||||
Type string `json:"type"`
|
||||
Question string `json:"question"`
|
||||
Answer string `json:"answer"`
|
||||
Cached bool `json:"cached"`
|
||||
}
|
||||
|
||||
type HandlerRecorderObject struct {
|
||||
Node string `json:"node,omitempty"`
|
||||
Service string `json:"service"`
|
||||
Network string `json:"network"`
|
||||
RemoteAddr string `json:"remote"`
|
||||
LocalAddr string `json:"local"`
|
||||
Host string `json:"host"`
|
||||
Client string `json:"client,omitempty"`
|
||||
HTTP *HTTPRecorderObject `json:"http,omitempty"`
|
||||
DNS *DNSRecorderObject `json:"dns,omitempty"`
|
||||
Err string `json:"err,omitempty"`
|
||||
Duration time.Duration `json:"duration"`
|
||||
Time time.Time `json:"time"`
|
||||
SID string `json:"sid"`
|
||||
}
|
||||
|
||||
func (p *HandlerRecorderObject) Record(ctx context.Context, r recorder.Recorder) error {
|
||||
if p == nil || r == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
data, err := json.Marshal(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return r.Record(ctx, data)
|
||||
}
|
||||
|
||||
@@ -5,11 +5,13 @@ import (
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/go-gost/core/logger"
|
||||
"github.com/go-gost/core/recorder"
|
||||
)
|
||||
|
||||
type tcpRecorderOptions struct {
|
||||
timeout time.Duration
|
||||
log logger.Logger
|
||||
}
|
||||
|
||||
type TCPRecorderOption func(opts *tcpRecorderOptions)
|
||||
@@ -20,9 +22,16 @@ func TimeoutTCPRecorderOption(timeout time.Duration) TCPRecorderOption {
|
||||
}
|
||||
}
|
||||
|
||||
func LogTCPRecorderOption(log logger.Logger) TCPRecorderOption {
|
||||
return func(opts *tcpRecorderOptions) {
|
||||
opts.log = log
|
||||
}
|
||||
}
|
||||
|
||||
type tcpRecorder struct {
|
||||
addr string
|
||||
dialer *net.Dialer
|
||||
log logger.Logger
|
||||
}
|
||||
|
||||
// TCPRecorder records data to TCP service.
|
||||
@@ -37,6 +46,7 @@ func TCPRecorder(addr string, opts ...TCPRecorderOption) recorder.Recorder {
|
||||
dialer: &net.Dialer{
|
||||
Timeout: options.timeout,
|
||||
},
|
||||
log: options.log,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user