-
Notifications
You must be signed in to change notification settings - Fork 0
/
freetaxii.go
executable file
·179 lines (143 loc) · 5.2 KB
/
freetaxii.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
// Copyright 2015 Bret Jordan, All rights reserved.
//
// Use of this source code is governed by an Apache 2.0 license
// that can be found in the LICENSE file in the root of the source
// tree.
package main
import (
"code.google.com/p/getopt"
"fmt"
"github.com/freetaxii/freetaxii-server/lib/config"
"github.com/freetaxii/freetaxii-server/lib/taxiiserver"
"log"
"net/http"
"os"
)
const (
DEFAULT_CONFIG_FILENAME = "etc/freetaxii.conf"
)
var sVersion = "0.2.1"
var sOptConfigFilename = getopt.StringLong("config", 'c', DEFAULT_CONFIG_FILENAME, "Configuration File", "string")
var bOptHelp = getopt.BoolLong("help", 0, "Help")
var bOptVer = getopt.BoolLong("version", 0, "Version")
func main() {
getopt.HelpColumn = 35
getopt.DisplayWidth = 120
getopt.SetParameters("")
getopt.Parse()
if *bOptVer {
printVersion()
}
if *bOptHelp {
printHelp()
}
// --------------------------------------------------
// Load System Configuration
// --------------------------------------------------
var syscfg config.ServerConfigType
syscfg.LoadConfig(*sOptConfigFilename)
// --------------------------------------------------
// Setup Logging File
// --------------------------------------------------
// TODO
// Need to make the directory if it does not already exist
// To do this, we need to split the filename from the directory, we will want to only
// take the last bit in case there is multiple directories /etc/foo/bar/stuff.log
// Only enable logging to a file if it is turned on in the configuration file
if syscfg.Logging.Enabled == true {
logFile, err := os.OpenFile(syscfg.Logging.LogFileFullPath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer logFile.Close()
log.SetOutput(logFile)
}
// --------------------------------------------------
// Setup Directory Path Handlers
// --------------------------------------------------
// Make sure there is a directory path defined in the configuration file
// for each service we want to listen on.
log.Println("Starting FreeTAXII Server")
serviceCounter := 0
// --------------------------------------------------
// Setup Server Object for a listeners
// --------------------------------------------------
var taxiiServerObject taxiiserver.ServerType
taxiiServerObject.SysConfig = &syscfg
// TODO need to add the ability to signal this process and change the flag
// from false to true so that it will be reloaded.
taxiiServerObject.ReloadServices = true
if syscfg.Logging.LogLevel >= 3 {
log.Println("DEBUG-3: Setting reload services to true")
}
// --------------------------------------------------
// Setup Discovery Server
// --------------------------------------------------
if syscfg.Services.Discovery != "" {
log.Println("Starting TAXII Discovery services at:", syscfg.Services.Discovery)
http.HandleFunc(syscfg.Services.Discovery, taxiiServerObject.DiscoveryServerHandler)
serviceCounter++
}
// --------------------------------------------------
// Setup Collection Server
// --------------------------------------------------
if syscfg.Services.Collection != "" {
log.Println("Starting TAXII Collection services at:", syscfg.Services.Collection)
http.HandleFunc(syscfg.Services.Collection, taxiiServerObject.CollectionServerHandler)
serviceCounter++
}
// --------------------------------------------------
// Setup Poll Server
// --------------------------------------------------
if syscfg.Services.Poll != "" {
log.Println("Starting TAXII Poll services at:", syscfg.Services.Poll)
http.HandleFunc(syscfg.Services.Poll, taxiiServerObject.PollServerHandler)
serviceCounter++
}
// --------------------------------------------------
// Setup Admin Server
// --------------------------------------------------
if syscfg.Services.Admin != "" {
log.Println("Starting TAXII Admin services at:", syscfg.Services.Admin)
http.HandleFunc(syscfg.Services.Admin, taxiiServerObject.AdminServerHandler)
//serviceCounter++ Do not count this service in the list
}
// --------------------------------------------------
// Fail if no services are running
// --------------------------------------------------
if serviceCounter == 0 {
log.Fatalln("No TAXII services defined")
}
// --------------------------------------------------
// Listen for Incoming Connections
// --------------------------------------------------
// TODO - Need to verify the list address is a valid IPv4 address and port
// combination.
if syscfg.System.Listen != "" {
http.ListenAndServe(syscfg.System.Listen, nil)
} else {
log.Fatalln("The listen directive is missing from the configuration file")
}
}
// --------------------------------------------------
// Print Help and Version infomration
// --------------------------------------------------
func printHelp() {
printOutputHeader()
getopt.Usage()
os.Exit(0)
}
func printVersion() {
printOutputHeader()
os.Exit(0)
}
// --------------------------------------------------
// Print a header for all output
// --------------------------------------------------
func printOutputHeader() {
fmt.Println("")
fmt.Println("FreeTAXII Server")
fmt.Println("Copyright, Bret Jordan")
fmt.Println("Version:", sVersion)
fmt.Println("")
}