-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
236 lines (220 loc) · 6.29 KB
/
main.go
File metadata and controls
236 lines (220 loc) · 6.29 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
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
package main
import (
"bytes"
"database/sql"
"encoding/hex"
"encoding/json"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
_ "github.com/mxk/go-sqlite/sqlite3"
)
type ClientConnection struct {
output http.ResponseWriter
parameters map[string]string
}
type UserLogin struct {
Username string
Password string
}
type Configuration struct {
ListenPort int
Database string
IgnoredArticles []string
Users []UserLogin
Folders []string
}
var subsonicVersion string = "1.2.0"
var responseHeaderOk string = `<?xml version="1.0" encoding="UTF-8"?><subsonic-response xmlns="http://subsonic.org/restapi" status="ok" version="` + subsonicVersion + `">`
var responseHeaderFail string = `<?xml version="1.0" encoding="UTF-8"?><subsonic-response xmlns="http://subsonic.org/restapi" status="failed" version="` + subsonicVersion + `">`
var responseFooter string = `</subsonic-response>`
var settings Configuration
func (connection *ClientConnection) responseSuccess( content string ) {
io.WriteString( connection.output, responseHeaderOk + content + responseFooter )
}
func (connection *ClientConnection) responseError( code int ) {
var message string
switch code {
case 0:
message = "Generic error."
case 10:
message = "Required parameter is missing."
case 20:
message = "Incompatible Subsonic REST protocol version. Client must upgrade."
case 30:
message = "Incompatible Subsonic REST protocol version. Server must upgrade."
case 40:
message = "Wrong username or password"
case 50:
message = "User is not authorized for the given operation."
case 70:
message = "The requested data was not found."
}
io.WriteString( connection.output, responseHeaderFail + `<error code="` + strconv.Itoa(code) + `" message="` + message + `"/>` + responseFooter )
}
func xmlString( text string ) string {
output := text
output = strings.Replace( output, "&", "&", -1 )
output = strings.Replace( output, "<", "<", -1 )
output = strings.Replace( output, ">", ">", -1 )
output = strings.Replace( output, "\"", """, -1 )
return output
}
func (connection *ClientConnection) processRequest( request string ) {
clientUsername := connection.parameters["u"]
clientPassword := connection.parameters["p"]
if strings.HasPrefix( clientPassword, "enc:" ) {
password,error := hex.DecodeString( clientPassword[4:] )
if error != nil {
println( "Error decoding password" )
connection.responseError( 40 )
return
}
clientPassword = string(password)
}
valid := false
for _,login := range settings.Users {
if login.Username == clientUsername && login.Password == clientPassword {
valid = true
break
}
}
if !valid {
println( "Invalid login" )
connection.responseError( 40 )
return
}
println( "API:", request )
for i,p := range connection.parameters {
if i != "u" && i != "p" && i != "v" && i != "c" {
println( " ", i, "=", p )
}
}
switch request {
case "/rest/ping.view":
connection.responseSuccess( "" )
case "/rest/getCoverArt.view":
connection.getCoverArt()
case "/rest/getIndexes.view":
connection.getIndexes()
case "/rest/getLicense.view":
connection.responseSuccess( `<license valid="true"/>` )
case "/rest/getMusicDirectory.view":
connection.getMusicDirectory()
case "/rest/getMusicFolders.view":
connection.getMusicFolders()
case "/rest/getRandomSongs.view":
connection.getRandomSongs()
case "/rest/getUser.view":
connection.getUser()
case "/rest/stream.view":
connection.stream()
default:
connection.responseError( 0 )
}
}
func subsonicServer( output http.ResponseWriter, request *http.Request ) {
var connection ClientConnection
connection.output = output
connection.parameters = make( map[string]string )
parameters := strings.Split( request.URL.RawQuery, "&" )
for _, item := range parameters {
if len(item) == 0 {
continue
}
items := strings.Split( item, "=" )
connection.parameters[items[0]] = items[1]
}
bodyBuffer := new( bytes.Buffer )
bodyBuffer.ReadFrom( request.Body )
parameters = strings.Split( bodyBuffer.String(), "&" )
for _, item := range parameters {
if len(item) == 0 {
continue
}
items := strings.Split( item, "=" )
connection.parameters[items[0]] = items[1]
}
request.Body.Close()
connection.processRequest( request.URL.Path )
}
func main() {
println( "Loading settings." )
jsonFile,error := os.Open( "settings.conf" )
defer jsonFile.Close()
if error != nil {
println( "Unable to read settings.", error.Error() )
return
}
jsonDecoder := json.NewDecoder( jsonFile )
error = jsonDecoder.Decode( &settings )
if error != nil {
println( "Unable to parse settings.", error.Error() )
return
}
println( "Checking database integrity." )
if _,error := os.Stat( settings.Database ); os.IsNotExist( error ) {
file,_ := os.Create( settings.Database )
file.Close()
}
db,error := sql.Open( "sqlite3", settings.Database )
if error != nil {
println( "Error opening database", error.Error() )
return
}
error = db.Ping()
if error != nil {
println( "Error accessing database", error.Error() )
return
}
_,error = db.Exec( `CREATE TABLE IF NOT EXISTS folders (
folderKey INTEGER PRIMARY KEY,
folderName TEXT NOT NULL,
folderPath TEXT NOT NULL,
folderParent INTEGER NOT NULL,
folderCreated INTEGER NOT NULL,
folderLastUpdate INTEGER NOT NULL,
folderRoot TEXT NOT NULL
);` )
if error != nil {
println( "Error creating 'folders' database table", error )
return
}
_,error = db.Exec( `CREATE TABLE IF NOT EXISTS songs (
songKey INTEGER PRIMARY KEY,
songPath TEXT NOT NULL,
songParent INTEGER NOT NULL,
songCreated INGETER NOT NULL,
songLastUpdate INTEGER NOT NULL,
songTitle TEXT NOT NULL,
songArtist TEXT NOT NULL,
songAlbum TEXT NOT NULL,
songDuration INTEGER NOT NULL,
songYear INTEGER NOT NULL,
songTrack INTEGER NOT NULL,
songDisc INTEGER NOT NULL
);` )
if error != nil {
println( "Error creating 'songs' database table", error )
return
}
db.Close()
go func() {
counter := 0
for {
println( "Enumerating music." )
scanMusicFolders( counter % 6 != 0 )
time.Sleep( 10 * time.Minute )
counter++
}
}()
println( "Starting server." )
http.HandleFunc( "/rest/", subsonicServer )
error = http.ListenAndServe( ":" + strconv.Itoa(settings.ListenPort), nil )
if error != nil {
println( "Fatal error, unable to start server.", error )
}
}