summaryrefslogtreecommitdiff
path: root/main.go
blob: 2ee02559243dc3ef2859a08cb083e2e62c499623 (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package main

import (
	"bytes"
	"errors"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"strings"
	"text/template"
	"time"

	"github.com/coreos/go-semver/semver"
)

var (
	addrFlag       = flag.String("addr", ":8080", "Serve HTTP at given address")
	socketFlag     = flag.String("socket", "", "Serve HTTP at given UNIX socket")
	vanityRootFlag = flag.String("vanity-root", "", "Vanity root URL (e.g.: https://upper.io).")
	repoRootFlag   = flag.String("repo-root", "", "Git repository root URL (e.g.: https://github.com/upper).")
)

var packagePattern = regexp.MustCompile(`^/([-a-zA-Z0-9]+)\.?(v([0-9]*))?(.*)$`)

var httpClient = &http.Client{Timeout: 10 * time.Second}

const refsSuffix = ".git/info/refs?service=git-upload-pack"

// Error messages.
var (
	ErrNoRepo    = errors.New("repository not found")
	ErrNoVersion = errors.New("version reference not found")
)

func main() {
	if err := run(); err != nil {
		log.Fatalf("Could not start server: %q", err)
	}
}

func run() error {
	flag.Parse()

	if *addrFlag == "" && *socketFlag == "" {
		return fmt.Errorf("must provide -addr")
	}

	if *repoRootFlag == "" {
		return fmt.Errorf("must provide -repo-root")
	}

	if *vanityRootFlag == "" {
		return fmt.Errorf("must provide -vanity-root")
	}

	repoRoot, err := NewRepoRoot(*repoRootFlag, *vanityRootFlag)
	if err != nil {
		return fmt.Errorf("could not parse -repo-root: %q", err)
	}

	var listenAddr, listenNet string

	if *socketFlag != "" {
		listenNet, listenAddr = "unix", *socketFlag
	} else {
		listenNet, listenAddr = "tcp", *addrFlag
	}

	li, err := net.Listen(listenNet, listenAddr)
	if err != nil {
		return fmt.Errorf("Failed to bind to %s %s: %v", listenNet, listenAddr, err)
	}

	http.HandleFunc("/", newHandler(repoRoot))

	log.Printf("Listening at %s. %s -> %s", listenAddr, *vanityRootFlag, *repoRootFlag)

	return http.Serve(li, nil)
}

var gogetTemplate = template.Must(template.New("").Parse(`
<html>
<head>
<meta name="go-import" content="{{.VanityPath}} git {{.VanityURL}}">
<meta name="go-source" content="{{.VanityPath}} _ {{.RepoRootURL}}/tree/{{.GitTree}}{/dir} {{.RepoRootURL}}/blob/{{.GitTree}}{/dir}/{file}#L{line}">
</head>
<body>
go get {{.VanityPath}}
</body>
</html>
`))

// RepoRoot represents a real repository and vanity name pair.
type RepoRoot struct {
	vanityURL      *url.URL
	repoURL        *url.URL
	RepoHostPath   string
	VanityHostPath string
}

func parseRepoURL(in string) (*url.URL, error) {
	u, err := url.Parse(in)
	if err != nil {
		return nil, err
	}
	if u.Scheme == "" {
		u.Scheme = "https"
	}
	return u, err
}

// NewRepoRoot creates a RepoRoot
func NewRepoRoot(repoURL string, vanityURL string) (*RepoRoot, error) {
	u, err := parseRepoURL(repoURL)
	if err != nil {
		return nil, err
	}

	v, err := parseRepoURL(vanityURL)
	if err != nil {
		return nil, err
	}

	return &RepoRoot{
		repoURL:        u,
		vanityURL:      v,
		RepoHostPath:   u.Host + u.Path,
		VanityHostPath: v.Host + v.Path,
	}, nil
}

// NewRepo creates a new repository.
func (root *RepoRoot) NewRepo(name string) *Repo {
	return &Repo{
		Root: root,
		Name: name,
	}
}

// Repo represents a source code repository on GitHub.
type Repo struct {
	Root *RepoRoot

	Name  string
	Major string

	RequestedVersion semver.Version

	// FullVersion is the best version in AllVersions that matches MajorVersion.
	// It defaults to InvalidVersion if there are no matches.
	FullVersion *semver.Version

	// AllVersions holds all versions currently available in the repository,
	// either coming from branch names or from tag names. Version zero (v0)
	// is only present in the list if it really exists in the repository.
	AllVersions semver.Versions
}

// SetVersions records in the relevant fields the details about which
// package versions are available in the repository.
func (repo *Repo) SetVersions(all semver.Versions) {
	repo.AllVersions = all
	for _, v := range repo.AllVersions {
		if v.Major == repo.RequestedVersion.Major && (repo.FullVersion == nil || repo.FullVersion.LessThan(*v)) {
			repo.FullVersion = v
		}
	}
}

// RepoRoot returns the repository root, without a schema.
func (repo *Repo) RepoRoot() string {
	return repo.Root.RepoHostPath + "/" + repo.Name
}

// VanityRoot returns the vanity repository root, without a schema.
func (repo *Repo) VanityRoot() string {
	return repo.Root.VanityHostPath + "/" + repo.Name
}

// GitTree returns the repository tree name for the selected version.
func (repo *Repo) GitTree() string {
	if repo.FullVersion == nil || repo.Major == "" {
		return "master"
	}
	return repo.FullVersion.String()
}

// VanityPath returns the real package path, without a schema.
func (repo *Repo) VanityPath() string {
	if repo.Major == "" {
		return repo.VanityRoot()
	}
	return repo.VanityRoot() + ".v" + repo.Major
}

// VanityURL returns the vanity package's URL.
func (repo *Repo) VanityURL() string {
	return repo.Root.vanityURL.Scheme + "://" + repo.VanityPath()
}

// RepoRootURL returns the real package's URL.
func (repo *Repo) RepoRootURL() string {
	return repo.Root.repoURL.Scheme + "://" + repo.RepoRoot()
}

func newHandler(repoRoot *RepoRoot) func(http.ResponseWriter, *http.Request) {
	return func(resp http.ResponseWriter, req *http.Request) {
		if req.URL.Path == "/health-check" {
			resp.Write([]byte("ok"))
			return
		}
		log.Printf("%s requested %s", req.RemoteAddr, req.URL)

		if req.URL.Path == "/" {
			sendNotFound(resp, "Missing package name.")
			return
		}

		u, err := url.Parse(req.URL.Path)
		if err != nil {
			sendError(resp, "Failed to parse request path")
			return
		}

		p := packagePattern.FindStringSubmatch(u.Path)

		pkgName, _, version, extra := p[1], p[2], p[3], p[4]
		repo := repoRoot.NewRepo(pkgName)

		var requestedVersion semver.Version
		if version != "" {
			repo.Major = version
			repo.RequestedVersion.Major, _ = strconv.ParseInt(repo.Major, 10, 64)
		}

		var changed []byte
		var versions semver.Versions
		original, err := fetchRefs(repo)
		if err == nil {
			changed, versions, err = changeRefs(original, &repo.RequestedVersion)
			repo.SetVersions(versions)
		}

		switch err {
		case nil:
			// all ok
		case ErrNoRepo:
			sendNotFound(resp, "Git repository not found at https://%s", repo.RepoRoot())
			return
		case ErrNoVersion:
			sendNotFound(resp, `Git repository at https://%s has no tag %v`, repo.RepoRoot(), requestedVersion)
			return
		default:
			resp.WriteHeader(http.StatusBadGateway)
			resp.Write([]byte(fmt.Sprintf("Cannot obtain refs from Git: %v", err)))
			return
		}

		switch extra {
		case `/git-upload-pack`:
			proxyURL := "https://" + repo.RepoRoot() + "/git-upload-pack"

			proxyReq, err := http.NewRequest(req.Method, proxyURL, req.Body)
			if err != nil {
				resp.WriteHeader(http.StatusInternalServerError)
				return
			}
			for k, v := range req.Header {
				proxyReq.Header[k] = v
			}

			proxyRes, err := http.DefaultClient.Do(proxyReq)
			if err != nil {
				log.Printf("Proxy: %v", err)
				resp.WriteHeader(http.StatusServiceUnavailable)
				return
			}
			defer proxyRes.Body.Close()

			for k, v := range proxyRes.Header {
				resp.Header()[k] = v
			}

			buf, err := ioutil.ReadAll(proxyRes.Body)
			if err != nil {
				log.Printf("Proxy: %v", err)
				resp.WriteHeader(http.StatusBadGateway)
				return
			}

			if _, err = resp.Write(buf); err != nil {
				log.Printf("Proxy: %v", err)
				resp.WriteHeader(http.StatusInternalServerError)
				return
			}
			return
		case `/info/refs`:
			resp.Header().Set("Content-Type", "application/x-git-upload-pack-advertisement")
			resp.Write(changed)
			return
		}

		resp.Header().Set("Content-Type", "text/html")
		if req.FormValue("go-get") == "1" {
			// execute simple template when this is a go-get request
			err = gogetTemplate.Execute(resp, repo)
			if err != nil {
				log.Printf("error executing go get template: %s\n", err)
			}
			return
		}

		sendNotFound(resp, "Missing ?go-get=1 parameter.")
	}
}

func sendError(resp http.ResponseWriter, msg string, args ...interface{}) {
	if len(args) > 0 {
		msg = fmt.Sprintf(msg, args...)
	}
	resp.WriteHeader(http.StatusInternalServerError)
	resp.Write([]byte(msg))
}

func sendNotFound(resp http.ResponseWriter, msg string, args ...interface{}) {
	if len(args) > 0 {
		msg = fmt.Sprintf(msg, args...)
	}
	resp.WriteHeader(http.StatusNotFound)
	resp.Write([]byte(msg))
}

func fetchRefs(repo *Repo) (data []byte, err error) {
	repoURL := repo.RepoRootURL() + refsSuffix
	resp, err := httpClient.Get(repoURL)
	if err != nil {
		return nil, fmt.Errorf("cannot talk to git repository: %v", err)
	}
	defer resp.Body.Close()

	switch resp.StatusCode {
	case 200:
		// ok
	case 401, 404:
		return nil, ErrNoRepo
	default:
		return nil, fmt.Errorf("error from git repository: %v", resp.Status)
	}

	data, err = ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("error reading from git: %v", err)
	}
	return data, err
}

func changeRefs(data []byte, major *semver.Version) (changed []byte, versions semver.Versions, err error) {
	var hlinei, hlinej int // HEAD reference line start/end
	var mlinei, mlinej int // master reference line start/end
	var vrefhash string
	var vrefname string
	var vrefv *semver.Version

	// Record all available versions, the locations of the master and HEAD lines,
	// and details of the best reference satisfying the requested major version.
	versions = semver.Versions{}
	sdata := string(data)
	for i, j := 0, 0; i < len(data); i = j {
		size, err := strconv.ParseInt(sdata[i:i+4], 16, 32)
		if err != nil {
			return nil, nil, fmt.Errorf("cannot parse refs line size: %s", string(data[i:i+4]))
		}
		if size == 0 {
			size = 4
		}
		j = i + int(size)
		if j > len(sdata) {
			return nil, nil, fmt.Errorf("incomplete refs data received from GitHub")
		}
		if sdata[0] == '#' {
			continue
		}

		hashi := i + 4
		hashj := strings.IndexByte(sdata[hashi:j], ' ')
		if hashj < 0 || hashj != 40 {
			continue
		}
		hashj += hashi

		namei := hashj + 1
		namej := strings.IndexAny(sdata[namei:j], "\n\x00")
		if namej < 0 {
			namej = j
		} else {
			namej += namei
		}

		name := sdata[namei:namej]

		if name == "HEAD" {
			hlinei = i
			hlinej = j
		}
		if name == "refs/heads/master" {
			mlinei = i
			mlinej = j
		}

		if strings.HasPrefix(name, "refs/tags/v") {
			if !strings.HasSuffix(name, "^{}") {
				continue // Only accept annotated tags.
			}
			// Annotated tag is peeled off and overrides the same version just parsed.
			name = name[:len(name)-3]

			v, err := semver.NewVersion(name[strings.IndexByte(name, 'v')+1:])
			if err == nil {
				versions = append(versions, v)
				if major.Major == v.Major && (vrefv == nil || v == vrefv || vrefv.LessThan(*v)) {
					vrefv = v
					vrefhash = sdata[hashi:hashj]
					vrefname = name
				}
			}
		}
	}

	// If the file has no HEAD line or the version was not found, report as unavailable.
	if hlinei == 0 || vrefhash == "" {
		return nil, nil, ErrNoVersion
	}

	var buf bytes.Buffer
	buf.Grow(len(data) + 256)

	// Copy the header as-is.
	buf.Write(data[:hlinei])

	// Extract the original capabilities.
	caps := ""
	if i := strings.Index(sdata[hlinei:hlinej], "\x00"); i > 0 {
		caps = strings.Replace(sdata[hlinei+i+1:hlinej-1], "symref=", "oldref=", -1)
	}

	// Insert the HEAD reference line with the right hash and a proper symref capability.
	var line string
	if strings.HasPrefix(vrefname, "refs/heads/") {
		if caps == "" {
			line = fmt.Sprintf("%s HEAD\x00symref=HEAD:%s\n", vrefhash, vrefname)
		} else {
			line = fmt.Sprintf("%s HEAD\x00symref=HEAD:%s %s\n", vrefhash, vrefname, caps)
		}
	} else {
		if caps == "" {
			line = fmt.Sprintf("%s HEAD\n", vrefhash)
		} else {
			line = fmt.Sprintf("%s HEAD\x00%s\n", vrefhash, caps)
		}
	}
	fmt.Fprintf(&buf, "%04x%s", 4+len(line), line)

	// Insert the master reference line.
	line = fmt.Sprintf("%s refs/heads/master\n", vrefhash)
	fmt.Fprintf(&buf, "%04x%s", 4+len(line), line)

	// Append the rest, dropping the original master line if necessary.
	if mlinei > 0 {
		buf.Write(data[hlinej:mlinei])
		buf.Write(data[mlinej:])
	} else {
		buf.Write(data[hlinej:])
	}

	return buf.Bytes(), versions, nil
}