-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
276 lines (221 loc) · 7.88 KB
/
main.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
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"runtime"
"strings"
)
type obsPackageFile struct {
URL string `json:"url"`
Version int `json:"version"`
Files []struct {
Name string `json:"name"`
Version int `json:"version"`
} `json:"files"`
}
type obsServicesFile struct {
FormatVersion int `json:"format_version"`
Services []interface{} `json:"services"`
}
type obsService struct {
Name string `json:"name"`
Servers []struct {
Name string `json:"name"`
URL string `json:"url"`
} `json:"servers"`
Recommended *struct {
Keyint *int `json:"keyint,omitempty"`
Profile string `json:"profile,omitempty"`
Output string `json:"output,omitempty"`
MaxVideoBitrate int `json:"max video bitrate,omitempty"`
MaxAudioBitrate int `json:"max audio bitrate,omitempty"`
Bframes *int `json:"bframes,omitempty"`
X264Opts string `json:"x264opts,omitempty"`
} `json:"recommended,omitempty"`
}
type logWriter struct {
}
func (writer logWriter) Write(bytes []byte) (int, error) {
return fmt.Print(string(bytes))
}
func panicAndPause(v ...interface{}) {
log.Print(v...)
fmt.Println("Glimesh OBS Service Patcher Failed!\nPress the Enter key or close this window.")
fmt.Scanln()
os.Exit(1)
}
func main() {
log.SetFlags(0)
log.SetOutput(new(logWriter))
var err error
glimeshRawFtlService := getGlimeshServiceContents("https://glimesh-static-assets.nyc3.digitaloceanspaces.com/obs-glimesh-service.json")
glimeshRawRtmpService := getGlimeshServiceContents("https://glimesh-static-assets.nyc3.digitaloceanspaces.com/obs-glimesh-rtmp-service.json")
var glimeshFtlService obsService
err = json.Unmarshal(glimeshRawFtlService, &glimeshFtlService)
if err != nil {
panicAndPause("Problem unmarshalling Glimesh FTL JSON entry.")
}
var glimeshRtmpService obsService
err = json.Unmarshal(glimeshRawRtmpService, &glimeshRtmpService)
if err != nil {
panicAndPause("Problem unmarshalling Glimesh RTMP JSON entry.")
}
log.Println()
servicePaths := findObsDirectories()
for _, servicePath := range servicePaths {
updateFromOfficialSource(servicePath)
}
log.Println()
for _, servicePath := range servicePaths {
patchFile(path.Join(servicePath, "services.json"), glimeshFtlService, glimeshRtmpService)
}
log.Println()
fmt.Println("Glimesh OBS Service Patcher Completed!\nPress the Enter key or close this window.")
fmt.Scanln()
}
func getGlimeshServiceContents(url string) []byte {
resp, err := http.Get(url)
if err != nil {
panicAndPause(err)
}
if resp.StatusCode != http.StatusOK {
panicAndPause("Got an error code from the CDN")
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panicAndPause(err)
}
log.Printf("💽 Downloaded Glimesh Service Definition from %s\n", url)
return data
}
func patchFile(filePath string, ftlService obsService, rtmpService obsService) {
servicesFile, err := os.Open(filePath)
if err != nil {
panicAndPause(err)
}
var services obsServicesFile
byteValue, _ := ioutil.ReadAll(servicesFile)
json.Unmarshal(byteValue, &services)
// Since the names conflict, check for a particular ingest now
foundGlimesh := strings.Contains(string(byteValue), "\"ingest.kord.live.glimesh.tv\"")
foundGlimeshRtmp := strings.Contains(string(byteValue), "Glimesh - RTMP")
fmt.Println(foundGlimesh)
fmt.Println(foundGlimeshRtmp)
servicesFile.Close()
hasChanges := false
if !foundGlimesh {
services.Services = append(services.Services, ftlService)
hasChanges = true
}
if !foundGlimeshRtmp {
services.Services = append(services.Services, rtmpService)
hasChanges = true
}
if hasChanges {
newContents, _ := customJSONMarshal(services)
err = os.WriteFile(filePath, newContents, 0644)
if err != nil {
log.Printf("⛔️ Failed to patch file: %s", filePath)
panicAndPause("⛔️ Please try running the program as an Administrator")
}
log.Printf("✅ Patched services file: %s", filePath)
log.Print("✅ Glimesh RTMP")
log.Print("✅ Patched FTL")
} else {
log.Printf("✅ Glimesh RTMP & FTL already exists in: %s", filePath)
}
}
func updateFromOfficialSource(servicePath string) {
jsonFile, err := os.Open(path.Join(servicePath, "package.json"))
if err != nil {
fmt.Println(err)
}
defer jsonFile.Close()
packageRaw, _ := ioutil.ReadAll(jsonFile)
var obsPackage obsPackageFile
json.Unmarshal(packageRaw, &obsPackage)
packageURL := obsPackage.URL + "/services.json"
resp, err := http.Get(packageURL)
if err != nil {
panicAndPause(err)
}
if resp.StatusCode != http.StatusOK {
panicAndPause(fmt.Sprintf("Got an error code from %s", packageURL))
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
panicAndPause(err)
}
servicesFile := path.Join(servicePath, "services.json")
err = os.WriteFile(servicesFile, data, 0644)
if err != nil {
log.Printf("⛔️ Failed to patch file: %s", servicesFile)
panicAndPause("⛔️ Please try running the program as an Administrator")
}
log.Printf("💽 Downloaded fresh services file from %s\n", packageURL)
}
func findObsDirectories() (services []string) {
configDir, err := os.UserConfigDir()
if err != nil {
panicAndPause(err)
}
// Traditional config based path, that beautiful open source projects like OBS Studio use
obsPath := path.Join(configDir, "obs-studio", "plugin_config", "rtmp-services")
slobsPath := path.Join(configDir, "slobs-client", "plugin_config", "rtmp-services")
if _, err := os.Stat(path.Join(obsPath, "services.json")); err == nil {
// OBS Studio Exists
log.Printf("🔍 Detected OBS Studio at: %s\n", obsPath)
services = append(services, obsPath)
}
if _, err := os.Stat(path.Join(slobsPath, "services.json")); err == nil {
// Streamlabs OBS Exists
log.Printf("🔍 Detected Streamlabs OBS at: %s\n", slobsPath)
services = append(services, slobsPath)
}
// Gross electron packaged non-config directories that we have to inject into
if runtime.GOOS == "windows" {
// Weird compiled electron path for Windows SLOBS
// C:\Program Files\Streamlabs OBS\resources\app.asar.unpacked\node_modules\obs-studio-node\data\obs-plugins\rtmp-services
slobs32bitPath := path.Join(os.Getenv("programfiles(x86)"), "Streamlabs OBS", "resources", "app.asar.unpacked", "node_modules", "obs-studio-node", "data", "obs-plugins", "rtmp-services")
slobs64bitPath := path.Join(os.Getenv("programfiles"), "Streamlabs OBS", "resources", "app.asar.unpacked", "node_modules", "obs-studio-node", "data", "obs-plugins", "rtmp-services")
if _, err := os.Stat(path.Join(slobs32bitPath, "services.json")); err == nil {
// OBS Studio Exists
log.Printf("🔍 Detected SLOBS Electron 32-bit at: %s\n", slobs32bitPath)
services = append(services, slobs32bitPath)
}
if _, err := os.Stat(path.Join(slobs64bitPath, "services.json")); err == nil {
// OBS Studio Exists
log.Printf("🔍 Detected SLOBS Electron 64-bit at: %s\n", slobs64bitPath)
services = append(services, slobs64bitPath)
}
}
if runtime.GOOS == "darwin" {
// Weird compiled electron path for Mac SLOBS
// /Applications/Streamlabs OBS.app/Contents/Resources/app.asar.unpacked/node_modules/obs-studio-node/data/obs-plugins/rtmp-services/services.json
slobsAppPath := path.Join("/", "Applications", "Streamlabs OBS.app", "Contents", "Resources", "app.asar.unpacked", "node_modules", "obs-studio-node", "data", "obs-plugins", "rtmp-services")
if _, err := os.Stat(path.Join(slobsAppPath, "services.json")); err == nil {
// OBS Studio Exists
log.Printf("🔍 Detected SLOBS Electron at: %s\n", slobsAppPath)
services = append(services, slobsAppPath)
}
}
return services
}
func customJSONMarshal(t interface{}) ([]byte, error) {
buffer := &bytes.Buffer{}
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
encoder.Encode(t)
var buf bytes.Buffer
err := json.Indent(&buf, buffer.Bytes(), "", " ")
if err != nil {
return nil, err
}
return buf.Bytes(), err
}