forked from michenriksen/gitrob
/
session.go
242 lines (210 loc) · 5.23 KB
/
session.go
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package core
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"runtime"
"sync"
"time"
"github.com/gin-gonic/gin"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
)
const (
AccessTokenEnvVariable = "GITROB_ACCESS_TOKEN"
StatusInitializing = "initializing"
StatusGathering = "gathering"
StatusAnalyzing = "analyzing"
StatusFinished = "finished"
)
type Stats struct {
sync.Mutex
StartedAt time.Time
FinishedAt time.Time
Status string
Progress float64
Targets int
Repositories int
Commits int
Files int
Findings int
}
type Session struct {
sync.Mutex
Version string
Options Options `json:"-"`
Out *Logger `json:"-"`
Stats *Stats
GithubAccessToken string `json:"-"`
GithubClient *github.Client `json:"-"`
Router *gin.Engine `json:"-"`
Targets []*GithubOwner
Repositories []*GithubRepository
Findings []*Finding
}
func (s *Session) Start() {
s.InitStats()
s.InitLogger()
s.InitThreads()
s.InitGithubAccessToken()
s.InitGithubClient()
s.InitRouter()
}
func (s *Session) Finish() {
s.Stats.FinishedAt = time.Now()
s.Stats.Status = StatusFinished
}
func (s *Session) AddTarget(target *GithubOwner) {
s.Lock()
defer s.Unlock()
for _, t := range s.Targets {
if *target.ID == *t.ID {
return
}
}
s.Targets = append(s.Targets, target)
}
func (s *Session) AddRepository(repository *GithubRepository) {
s.Lock()
defer s.Unlock()
for _, r := range s.Repositories {
if *repository.ID == *r.ID {
return
}
}
s.Repositories = append(s.Repositories, repository)
}
func (s *Session) AddFinding(finding *Finding) {
s.Lock()
defer s.Unlock()
s.Findings = append(s.Findings, finding)
}
func (s *Session) InitStats() {
if s.Stats != nil {
return
}
s.Stats = &Stats{
StartedAt: time.Now(),
Status: StatusInitializing,
Progress: 0.0,
Targets: 0,
Repositories: 0,
Commits: 0,
Files: 0,
Findings: 0,
}
}
func (s *Session) InitLogger() {
s.Out = &Logger{}
s.Out.SetDebug(*s.Options.Debug)
s.Out.SetSilent(*s.Options.Silent)
}
func (s *Session) InitGithubAccessToken() {
if *s.Options.GithubAccessToken == "" {
accessToken := os.Getenv(AccessTokenEnvVariable)
if accessToken == "" {
s.Out.Fatal("No GitHub access token given. Please provide via command line option or in the %s environment variable.\n", AccessTokenEnvVariable)
}
s.GithubAccessToken = accessToken
} else {
s.GithubAccessToken = *s.Options.GithubAccessToken
}
}
func (s *Session) InitGithubClient() {
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: s.GithubAccessToken},
)
tc := oauth2.NewClient(ctx, ts)
s.GithubClient = github.NewClient(tc)
s.GithubClient.UserAgent = fmt.Sprintf("%s v%s", Name, Version)
}
func (s *Session) InitThreads() {
if *s.Options.Threads == 0 {
numCPUs := runtime.NumCPU()
s.Options.Threads = &numCPUs
}
runtime.GOMAXPROCS(*s.Options.Threads + 2) // thread count + main + web server
}
func (s *Session) InitRouter() {
bind := fmt.Sprintf("%s:%d", *s.Options.BindAddress, *s.Options.Port)
s.Router = NewRouter(s)
go func(sess *Session) {
if err := sess.Router.Run(bind); err != nil {
sess.Out.Fatal("Error when starting web server: %s\n", err)
}
}(s)
}
func (s *Session) SaveToFile(location string) error {
sessionJson, err := json.Marshal(s)
if err != nil {
return err
}
err = ioutil.WriteFile(location, sessionJson, 0644)
if err != nil {
return err
}
return nil
}
func (s *Stats) IncrementTargets() {
s.Lock()
defer s.Unlock()
s.Targets++
}
func (s *Stats) IncrementRepositories() {
s.Lock()
defer s.Unlock()
s.Repositories++
}
func (s *Stats) IncrementCommits() {
s.Lock()
defer s.Unlock()
s.Commits++
}
func (s *Stats) IncrementFiles() {
s.Lock()
defer s.Unlock()
s.Files++
}
func (s *Stats) IncrementFindings() {
s.Lock()
defer s.Unlock()
s.Findings++
}
func (s *Stats) UpdateProgress(current int, total int) {
s.Lock()
defer s.Unlock()
if current >= total {
s.Progress = 100.0
} else {
s.Progress = (float64(current) * float64(100)) / float64(total)
}
}
func NewSession() (*Session, error) {
var err error
var session Session
if session.Options, err = ParseOptions(); err != nil {
return nil, err
}
if *session.Options.Save != "" && FileExists(*session.Options.Save) {
return nil, errors.New(fmt.Sprintf("File: %s already exists.", *session.Options.Save))
}
if *session.Options.Load != "" {
if !FileExists(*session.Options.Load) {
return nil, errors.New(fmt.Sprintf("Session file %s does not exist or is not readable.", *session.Options.Load))
}
data, err := ioutil.ReadFile(*session.Options.Load)
if err != nil {
return nil, err
}
if err := json.Unmarshal(data, &session); err != nil {
return nil, errors.New(fmt.Sprintf("Session file %s is corrupt or generated by an old version of Gitrob.", *session.Options.Load))
}
}
session.Version = Version
session.Start()
return &session, nil
}