Skip to content

Commit

Permalink
additional functions for fwrules and defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
imgurbot12 committed Sep 14, 2017
1 parent 11f5297 commit 492b30d
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,57 @@ package goaway2

import (
"database/sql"
"fmt"
"log"
"os"

_ "github.com/mattn/go-sqlite3" //mysql-driver
)

//TODO: need to change database location for when its compiled to something else
//TODO:50 need to change database location for when its compiled to something else

/***Varaibles***/
var db *sql.DB

/***Functions***/

//sqlLoadRules : load all firewall rules from database
func sqlLoadRules() (fwRules []*fwRule) {
// do sql query
rows, err := db.Query("SELECT Zone,FromIP,FromPort,ToIP,ToPort FROM rules ORDER BY RuleNum")
if err != nil {
fmt.Printf("Unable to collect firewall Rules! SQL-Error: %s", err.Error())
os.Exit(1)
}
// fill rules with given data
var rec *fwRaw
for rows.Next() {
rec = new(fwRaw)
rows.Scan(&rec.Zone, &rec.FromIP, &rec.FromPort, &rec.ToIP, &rec.ToPort)
// build rule with types based on data from sql table
fwRules = append(fwRules, &fwRule{
Zone: zone(rec.Zone),
SrcIP: convertIPs(rec.FromIP),
SrcPort: convertPorts(rec.FromPort),
DstIP: convertIPs(rec.ToIP),
DstPort: convertPorts(rec.ToPort),
})
}
rows.Close()
return fwRules
}

//sqlLoadDefaults : load rule options into defaults
func sqlLoadDefaults() (df *dfaults) {
// do sql query and scan data
err := db.QueryRow("SELECT Inbound, OutBound FROM ruleopts LIMIT 1").Scan(&df.inbound, &df.outbound)
if err != nil {
fmt.Printf("Unable to collect firewall options! SQL-Error: %s", err.Error())
os.Exit(1)
}
return df
}

//checkExists : check if given database exists
func checkExists(db *sql.DB, table string) {
rows, err := db.Query("SELECT 1 FROM " + table)
Expand All @@ -36,5 +75,7 @@ func init() {
checkExists(db, "rules")
checkExists(db, "whitelist")
checkExists(db, "blacklist")
// load fw rules from sql table
sqlLoadRules()
db.Close()
}

0 comments on commit 492b30d

Please sign in to comment.