-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.go
131 lines (122 loc) · 2.86 KB
/
db.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
// sdnsweb database access.
package main
import (
"database/sql"
"errors"
_ "github.com/go-sql-driver/mysql"
"strconv"
"time"
)
var (
dbcon *sql.DB = nil
)
func dbInit(hostname, username, password, dbname string) error {
var (
err error
)
if dbcon != nil {
dbcon.Close()
}
dsn := username + ":" + password + "@tcp(" + hostname + ":" + strconv.Itoa(sqlPort) + ")/" + dbname
dbcon, err = sql.Open("mysql", dsn)
return err
}
func dbRegisterDomain(domain string, dstIP string, accessKey string, submitIP string) error {
var (
err error
)
exists, err := dbDomainExists(domain)
if err != nil {
return err
}
if exists {
return errors.New("Domain already exists")
}
query := "insert into host_registry (hostname, access_key, submit_ip, locked, created) values (?, ?, ?, ?, ?)"
_, err = dbcon.Exec(query, domain, accessKey, submitIP, 0, time.Now().Unix())
if err != nil {
return err
}
query = "insert into host_records (hostname, dst_ip, submit_ip, updated, created) values (?, ?, ?, ?, ?)"
now := time.Now().Unix()
_, err = dbcon.Exec(query, domain, dstIP, submitIP, 0, now)
if err != nil {
return err
}
return nil
}
func dbGetAccessKey(domain string) (string, error) {
var (
key string
err error
query string = "select access_key from host_registry where hostname=?"
)
err = dbcon.QueryRow(query, domain).Scan(&key)
switch {
case err == sql.ErrNoRows:
err = nil
key = ""
case err != nil:
key = ""
}
return key, err
}
func dbDomainExists(domain string) (bool, error) {
key, err := dbGetAccessKey(domain)
if err != nil {
return false, err
}
if len(key) == 0 {
return false, nil
}
return true, nil
}
func dbGetDomainDstIP(domain string) (string, error) {
var (
dstIP string
err error
query string = "select dst_ip from host_records where hostname=?"
)
err = dbcon.QueryRow(query, domain).Scan(&dstIP)
switch {
case err == sql.ErrNoRows:
err = nil
dstIP = ""
case err != nil:
dstIP = ""
}
return dstIP, err
}
func dbUpdateDomain(domain string, dstIP string, accessKey string, submitIP string) (bool, error) {
var (
query string
)
dbKey, err := dbGetAccessKey(domain)
if err != nil {
return false, err
}
if len(dbKey) == 0 || accessKey != dbKey {
return false, errors.New("Invalid key")
}
dbDstIP, err := dbGetDomainDstIP(domain)
if err != nil {
return false, err
}
if dbDstIP == dstIP {
return false, nil
} else if dbDstIP != "" {
query = "update host_records set dst_ip=?, submit_ip=?, updated=? where hostname=?"
_, err := dbcon.Exec(query, dstIP, submitIP, time.Now().Unix(), domain)
if err != nil {
return false, err
}
} else {
query = "insert into host_records (hostname, dst_ip, submit_ip, updated, created) values (?, ?, ?, ?, ?)"
now := time.Now().Unix()
_, err = dbcon.Exec(query, domain, dstIP, submitIP, now, now)
if err != nil {
return false, err
}
}
return true, err
}