-
Notifications
You must be signed in to change notification settings - Fork 1
/
createPortMapDB.go
78 lines (67 loc) · 1.7 KB
/
createPortMapDB.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
package main
import (
"database/sql"
"fmt"
_ "github.com/mattn/go-sqlite3"
"log"
"net/http"
"encoding/csv"
"strconv"
"os"
"io"
)
func fetchCsv(db *sql.DB ) {
resp, _ := http.Get("https://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.csv")
defer resp.Body.Close()
r := csv.NewReader(resp.Body)
r.Read()
for {
record, err := r.Read()
if err == io.EOF {
break
}
serviceName := record[0]
if len(serviceName) > 0 {
portNumber, _ := strconv.ParseInt(record[1], 10, 32)
transportProtocol := record[2]
serviceDescription := record[3]
write(db, int(portNumber), serviceName, serviceDescription, transportProtocol)
fmt.Printf("service: %s port: %s, tr: %s, descr: %s", serviceName, portNumber, transportProtocol, serviceDescription)
fmt.Println(record)
}
}
}
func write(db *sql.DB, port int, service string, description string, transportProtocol string) {
tx, err := db.Begin()
if err != nil {
log.Fatal(err)
}
stmt, err := tx.Prepare("insert into portmap (name, port, description, transport) values(?, ?, ?, ?)")
if err != nil {
log.Fatal(err)
}
defer stmt.Close()
_, err = stmt.Exec(service, port, description, transportProtocol)
if err != nil {
log.Fatal(err)
}
tx.Commit()
}
func main() {
dbFile := "./app/src/main/assets/port_map.db"
os.Remove(dbFile)
db, err := sql.Open("sqlite3", dbFile)
if err != nil {
log.Fatal(err)
}
defer db.Close()
sqlStmt := `
create table portmap (portmapId INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT not null, port INT not null, description Text not null, transport Text not null);
`
_, err = db.Exec(sqlStmt)
if err != nil {
log.Printf("%q: %s\n", err, sqlStmt)
return
}
fetchCsv(db)
}