/
startSaveValidationHelper.go
190 lines (164 loc) · 5.28 KB
/
startSaveValidationHelper.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
package views
import (
"encoding/xml"
"fmt"
"math"
"regexp"
"sort"
"strconv"
"strings"
"github.com/labstack/echo/v4"
"github.com/ystv/streamer/server/helper"
)
func (v *Views) startSaveValidationHelper(c echo.Context, valType ValidationType) StartSaveValidationResponse {
var response StartSaveValidationResponse
var input string
switch valType {
case startValidation:
input = c.FormValue("stream_selector")
if len(input) < 3 {
response.Error = fmt.Errorf("invalid stream selector value: %s", input)
return response
}
case startUniqueValidation:
inputEndpoint := c.FormValue("endpoints_table")
inputStream := c.FormValue("stream_input")
streamPageContent, err := helper.GetBody("http://" + v.conf.StreamServer + "stat")
if err != nil {
response.Error = fmt.Errorf("failed to get streams from stream server: %+v", err)
return response
}
var rtmp RTMP
err = xml.Unmarshal([]byte(streamPageContent), &rtmp)
if err != nil {
response.Error = fmt.Errorf("failed to unmarshal xml: %+v", err)
return response
}
found := false
applicationFor:
for _, application := range rtmp.Server.Applications {
if application.Name == inputEndpoint {
for _, stream := range application.Live.Streams {
if stream.Name == inputStream {
found = true
input = inputEndpoint + "/" + stream.Name
break applicationFor
}
}
}
}
if !found {
response.Error = fmt.Errorf("unable to find current stream input")
return response
}
case saveValidation:
endpoint := c.FormValue("endpoints_table")
if len(endpoint) < 2 {
response.Error = fmt.Errorf("invalid endpoint selected: %s", endpoint)
return response
}
streamInput := c.FormValue("stream_input")
if len(streamInput) < 2 {
response.Error = fmt.Errorf("invalid stream input: %s", streamInput)
return response
}
input = fmt.Sprintf("%s/%s", endpoint, streamInput)
default:
response.Error = fmt.Errorf("invalid validation type")
return response
}
recordCheckboxRaw := c.FormValue("record_checkbox")
if recordCheckboxRaw != "" && recordCheckboxRaw != "on" {
response.Error = fmt.Errorf("invalid record checkbox value: %s", recordCheckboxRaw)
return response
}
recordCheckbox := recordCheckboxRaw == "on"
savePath := c.FormValue("save_path")
if len(savePath) == 0 && recordCheckbox {
response.Error = fmt.Errorf("invalid save path value")
return response
}
if recordCheckbox && !strings.HasSuffix(savePath, ".mkv") {
response.Error = fmt.Errorf("the save path must end in \".mkv\"")
return response
}
websiteCheckboxRaw := c.FormValue("website_stream")
if websiteCheckboxRaw != "" && websiteCheckboxRaw != "on" {
response.Error = fmt.Errorf("invalid website stream checkbox value: %s", recordCheckboxRaw)
return response
}
websiteCheckbox := websiteCheckboxRaw == "on"
websiteStreamEndpoint := c.FormValue("website_stream_endpoint")
if websiteCheckbox && !strings.Contains(websiteStreamEndpoint, "?pwd=") {
response.Error = fmt.Errorf("the website stream endpoint must contain \"?pwd=\"")
return response
}
var websiteOut string
if websiteCheckbox {
if v.websiteCheck(websiteStreamEndpoint) {
websiteOut = websiteStreamEndpoint
} else {
response.Error = fmt.Errorf("website key check has failed")
return response
}
}
// This section finds the number of the stream from the form
// You can miss values out, and some rearranging will have to be done
largest := 0
var numbers []int
for s := range c.Request().PostForm {
if strings.Contains(s, "stream_server_") {
split := strings.Split(s, "_")
conv, _ := strconv.ParseInt(split[2], 10, 64)
largest = int(math.Max(float64(largest), float64(conv)))
numbers = append(numbers, int(conv))
}
}
sort.Ints(numbers)
streamServerRegex, err := regexp.Compile("^(rtmps?:\\/\\/)?" + // protocol
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name
"((\\d{1,3}\\.){3}\\d{1,3}))" + // OR ip (v4) address
"(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*" + // port and path
"(\\?[;&a-z\\d%_.~+=-]*)?" + // query string
"(\\#[-a-z\\d_]*)?$") // fragment locator
if err != nil {
response.Error = fmt.Errorf("failed to compile regex: %+v", err)
return response
}
var streams []string
for _, index := range numbers {
streamServer := c.FormValue("stream_server_" + strconv.Itoa(index))
if len(streamServer) == 0 {
response.Error = fmt.Errorf("invalid length of stream_server_%d", index)
return response
}
if !streamServerRegex.Match([]byte(streamServer)) {
response.Error = fmt.Errorf("invalid value of stream_server_%d: %+v", index, err)
return response
}
if streamServer[len(streamServer)-1] != '/' {
streamServer += "/"
}
if valType == saveValidation {
streamServer += "|"
}
streamKey := c.FormValue("stream_key_" + strconv.Itoa(index))
if len(streamKey) == 0 {
response.Error = fmt.Errorf("invalid length of stream_key_%d", index)
return response
}
streamServer += streamKey
streams = append(streams, streamServer)
}
if len(streams) == 0 {
response.Error = fmt.Errorf("invalid length of streams")
return response
}
response.Input = input
response.RecordCheckbox = recordCheckbox
response.SavePath = savePath
response.WebsiteCheckbox = websiteCheckbox
response.WebsiteOut = websiteOut
response.Streams = streams
return response
}