~jzs/aproxy

ref: 385e0957d78566cdbf2ced486c9253bbcfc0a5ef aproxy/aproxy/tracker.go -rw-r--r-- 2.8 KiB
385e0957 — Jens Zeilund Sørensen Made aproxygo configurable only 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package aproxy

import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"net/http"
	"os"
	"path"
	"sort"
	"strings"
	"sync"
	"time"
)

// Event represents a tracking event
type Event struct {
	TS             time.Time
	URL            string
	RemoteAddr     string
	UserAgent      string
	AcceptLanguage string
	Referer        string
	Method         string
	Host           string
}

// Tracker represents a tracker that logs info about http requests and logs them
type Tracker struct {
	c      chan Event
	wg     sync.WaitGroup
	buffer bytes.Buffer
}

func NewTracker() *Tracker {
	t := &Tracker{}
	t.c = make(chan Event, 100000)
	//t.done = make(chan bool, 1)
	t.wg = sync.WaitGroup{}
	os.MkdirAll("/var/log/aproxy", 0700)
	// Create func that processes events.
	//  start tracking...
	t.wg.Add(1)
	go func() {
		for evt := range t.c {
			// Handle event.
			data, err := json.Marshal(evt)
			if err != nil {
				fmt.Println("Boom")
				continue
			}
			t.buffer.Write(data)
			t.buffer.Write([]byte("\n"))

			// Roll buffer if too big... (2MB at most)
			if t.buffer.Len() > 1024*1024*2 {
				t.flush()
			}
		}
		t.wg.Done()
	}()
	return t
}

// flush flushes the buffer to disk.
func (t *Tracker) flush() {
	dirPath := "/var/log/aproxy"
	dir, err := os.Open(dirPath)
	if err != nil {
		return
	}
	infos, err := dir.Readdir(0)
	if err != nil {
		return
	}

	// Compute today string.
	today := time.Now().Format("2006-01-02")

	tfiles := []os.FileInfo{}

	// parse names. Find correct file.
	for _, i := range infos {
		// Handle infos...
		if strings.HasPrefix(i.Name(), today) {
			tfiles = append(tfiles, i)
		}
	}

	var file *os.File
	if len(tfiles) == 0 {
		// Create a new file.
		file, err = os.Create(path.Join(dirPath, today+"-1.log"))
		if err != nil {
			panic(err)
			return
		}
	} else {
		sort.Slice(tfiles, func(i, j int) bool {
			return tfiles[i].Name() > tfiles[j].Name()
		})
		if tfiles[0].Size() > 1024*1024*50 {
			file, err = os.Create(path.Join(dirPath, fmt.Sprintf("%v-%v.log", today, len(tfiles)+1)))
			if err != nil {
				panic(err)
				return
			}
		} else {
			file, err = os.OpenFile(path.Join(dirPath, tfiles[0].Name()), os.O_APPEND|os.O_WRONLY, 0666)
			if err != nil {
				panic(err)
				return
			}
		}
	}
	defer file.Close()

	file.Write(t.buffer.Bytes())

	t.buffer.Reset()
}

func (p *Tracker) Shutdown() {
	close(p.c)
	p.wg.Wait()

	p.flush()
}

// Perform logging...
func (p *Tracker) Track(ctx context.Context, r *http.Request) {
	// Consider respecting DNT header?
	h := r.Header
	evt := Event{
		TS:             time.Now(),
		URL:            r.URL.String(),
		RemoteAddr:     r.RemoteAddr,
		UserAgent:      strings.Join(h["User-Agent"], "|"),
		AcceptLanguage: strings.Join(h["Accept-Language"], "|"),
		Referer:        strings.Join(h["Referer"], "|"),
		Method:         r.Method,
		Host:           r.Host,
	}
	p.c <- evt
}