master
Raw Download raw file
 1package main
 2
 3import (
 4	"crypto/tls"
 5	"crypto/x509"
 6	"flag"
 7	"github.com/BurntSushi/toml"
 8	"io/ioutil"
 9	"log"
10	"net/http"
11	"os"
12	"strconv"
13	"strings"
14)
15
16// CLIENT_CONFIG determines how the client is configured
17type CLIENT_CONFIG struct {
18	// SERVER is the IP or domain address of the server
19	SERVER string
20	// PORT is the port on which to connect
21	PORT int
22	// SERVER_URL contains the url to access the server, including the protocol
23	SERVER_URL string
24	// USE_HTTPS determines if the client should connect using HTTPS
25	USE_HTTPS bool
26	// USER defines the user that is in control of this session
27	USER string
28}
29
30var clientConfig CLIENT_CONFIG
31var client http.Client
32
33// loadClientConfiguration loads the configuration for the client using the given string
34func loadClientConfiguration(cfgFile string) CLIENT_CONFIG {
35	log.Printf("Loading configuration from %s.", cfgFile)
36	_, err := toml.DecodeFile(cfgFile, &clientConfig)
37	if err != nil {
38		log.Fatalf("Could not read configuration file: %+v\n", err.Error())
39		os.Exit(EXIT_CODE.FILE_IO_ERROR)
40	}
41	protocol := "http://"
42	if clientConfig.USE_HTTPS {
43		protocol = "https://"
44	}
45	clientConfig.SERVER_URL = protocol + clientConfig.SERVER + ":" + strconv.Itoa(clientConfig.PORT)
46	return clientConfig
47}
48
49// setupHttpClient initializes the http connection handler
50func setupHttpClient() {
51	ca_pool := x509.NewCertPool()
52	pemCert, _ := ioutil.ReadFile("/Users/RLuby/dev/golang/src/richluby/questioner/cert.pem")
53	ca_pool.AppendCertsFromPEM(pemCert)
54	tlsConfig := tls.Config{
55		ClientCAs:          ca_pool,
56		InsecureSkipVerify: true}
57	transport := &http.Transport{
58		TLSClientConfig: &tlsConfig}
59	client = http.Client{Transport: transport}
60}
61
62// initializes the client and handles the user interaction
63func ExecuteClient() {
64	clientConfig.SERVER = "127.0.0.1"
65	clientConfig.PORT = 80
66	clientConfig.USE_HTTPS = false
67	setupHttpClient()
68	filePath := flag.String("file", "", "defines a path to the configuration file")
69	flag.Parse()
70	if strings.Compare(*filePath, "") != 0 {
71		clientConfig = loadClientConfiguration(*filePath)
72	}
73	log.Printf("Configuration: %+v", clientConfig)
74
75	initUserSession()
76}