Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
138 lines (122 loc) · 2.98 KB
/
Copy pathmain.go
File metadata and controls
138 lines (122 loc) · 2.98 KB
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
package main
import (
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"unicode"
)
var (
LatestVersionFile = "/wire/otas/latest"
DoNotAcceptFile = "/wire/otas/dnar"
FullPath = "/wire/otas/full"
)
var TargetMap = []string{"dev", "oskr", "whiskey", "orange", "dvt3", "dvt2"}
func fileExists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
func shouldNotAccept() bool {
return fileExists(DoNotAcceptFile)
}
func normVer(v string) string {
var r []rune
for _, c := range v {
if !unicode.IsLetter(c) {
r = append(r, c)
}
}
return strings.TrimSpace(string(r))
}
func getLatestVersion() string {
b, err := os.ReadFile(LatestVersionFile)
if err != nil {
return ""
}
return strings.TrimSpace(string(b))
}
func targetToString(t string) string {
i, err := strconv.Atoi(t)
if err != nil || i < 0 || i >= len(TargetMap) {
return ""
}
return TargetMap[i]
}
func shouldInhibit(version string) bool {
return version == "2.1.0.18"
}
func getOTA(version, target string, auto bool) ([]byte, error) {
if shouldNotAccept() {
return nil, errors.New("server busy")
}
version = normVer(version)
fmt.Println(version)
if shouldInhibit(version) && auto {
return nil, errors.New("version was inhibited")
}
latest := getLatestVersion()
if version == latest {
return nil, errors.New("already on latest")
}
p := filepath.Join(FullPath, target, latest+".ota")
if fileExists(p) {
return os.ReadFile(p)
}
return nil, errors.New("ota not found")
}
func otaHandler(w http.ResponseWriter, r *http.Request) {
if shouldNotAccept() && r.FormValue("isBuildServer") != "true" {
http.Error(w, "busy uploading", 500)
return
}
path := r.URL.Path
if strings.HasPrefix(path, "/vic/full") || strings.HasPrefix(path, "/vic/diff") {
ver := normVer(r.FormValue("victorversion"))
t := r.FormValue("victortarget")
fmt.Println(ver, t)
if t == "" {
http.Error(w, "no target", 404)
return
}
t = targetToString(t)
var isAuto bool
if strings.HasPrefix(path, "/vic/diff") {
isAuto = true
}
data, err := getOTA(ver, t, isAuto)
if err != nil {
http.Error(w, err.Error(), 404)
return
}
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(data)))
w.Write(data)
return
}
if strings.HasPrefix(path, "/vic/latestVersion") {
l := getLatestVersion()
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(l)))
w.Write([]byte(l))
return
}
if strings.HasPrefix(path, "/vic/latest/") {
target := strings.Split(strings.Split(path, "/vic/latest/")[1], ".ota")[0]
latestOta, err := os.ReadFile(filepath.Join(FullPath, target, getLatestVersion()+".ota"))
if err != nil {
http.Error(w, "target not found", 404)
return
}
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(latestOta)))
w.Write(latestOta)
return
}
http.NotFound(w, r)
}
func main() {
http.HandleFunc("/vic/", otaHandler)
http.Handle("/", http.FileServer(http.Dir(FullPath+"/")))
fmt.Println("listening on :5901")
http.ListenAndServe(":5901", nil)
}