Commit 794b6a6a authored by Sietse Ringers's avatar Sietse Ringers
Browse files

Move helper functions for reuse

parent 9ded0c15
......@@ -2,13 +2,18 @@ package server
import (
"encoding/json"
"errors"
"net"
"net/http"
"os"
"path/filepath"
"runtime"
"runtime/debug"
"github.com/Sirupsen/logrus"
"github.com/go-errors/errors"
"github.com/privacybydesign/gabi"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/fs"
)
var Logger *logrus.Logger = logrus.StandardLogger()
......@@ -127,3 +132,74 @@ func ParseSessionRequest(bts []byte) (request irma.SessionRequest, err error) {
}
return nil, errors.New("Invalid or disabled session type")
}
func LocalIP() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("No IP found")
}
func CachePath() (string, error) {
candidates := make([]string, 0, 2)
if runtime.GOOS != "windows" {
candidates = append(candidates, filepath.Join("/var/tmp", "irmaserver"))
}
candidates = append(candidates, filepath.Join(os.TempDir(), "irmaserver"))
path := firstWritablePath(candidates)
if path == "" {
return "", errors.New("No writable temporary directory found")
}
return path, nil
}
func firstWritablePath(paths []string) string {
for _, path := range paths {
if err := fs.EnsureDirectoryExists(path); err != nil {
continue
}
return path
}
return ""
}
func Verbosity(level int) logrus.Level {
switch {
case level == 1:
return logrus.DebugLevel
case level > 1:
return logrus.TraceLevel
default:
return logrus.InfoLevel
}
}
package core
import (
"os"
"path/filepath"
"runtime"
"strconv"
"time"
......@@ -11,7 +8,6 @@ import (
"github.com/go-errors/errors"
"github.com/privacybydesign/gabi"
"github.com/privacybydesign/irmago"
"github.com/privacybydesign/irmago/internal/fs"
"github.com/privacybydesign/irmago/server"
)
......@@ -124,26 +120,3 @@ func chooseProtocolVersion(min, max *irma.ProtocolVersion) (*irma.ProtocolVersio
return max, nil
}
}
func CachePath() (string, error) {
candidates := make([]string, 0, 2)
if runtime.GOOS != "windows" {
candidates = append(candidates, filepath.Join("/var/tmp", "irmaserver"))
}
candidates = append(candidates, filepath.Join(os.TempDir(), "irmaserver"))
path := firstWritablePath(candidates)
if path == "" {
return "", errors.New("No writable temporary directory found")
}
return path, nil
}
func firstWritablePath(paths []string) string {
for _, path := range paths {
if err := fs.EnsureDirectoryExists(path); err != nil {
continue
}
return path
}
return ""
}
......@@ -4,6 +4,8 @@ import (
"crypto/rsa"
"fmt"
"io/ioutil"
"regexp"
"strconv"
"strings"
"github.com/dgrijalva/jwt-go"
......@@ -159,6 +161,9 @@ func (conf *Configuration) initialize() error {
conf.URL = conf.URL + "/"
}
conf.URL = conf.URL + "irma/"
// replace "port" in url with actual port
replace := "$1:" + strconv.Itoa(conf.Port)
conf.URL = string(regexp.MustCompile("(https?://[^/]*):port").ReplaceAll([]byte(conf.URL), []byte(replace)))
}
return nil
......
......@@ -4,17 +4,13 @@ package main
import (
"encoding/json"
"io/ioutil"
"net"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/Sirupsen/logrus"
"github.com/go-errors/errors"
"github.com/privacybydesign/irmago/server"
"github.com/privacybydesign/irmago/server/core"
"github.com/privacybydesign/irmago/server/irmaserver"
"github.com/spf13/cobra"
"github.com/spf13/viper"
......@@ -61,11 +57,11 @@ func setFlags(cmd *cobra.Command) error {
flags := cmd.Flags()
flags.SortFlags = false
cachepath, err := core.CachePath()
cachepath, err := server.CachePath()
if err != nil {
return err
}
defaulturl, err := localIP()
defaulturl, err := server.LocalIP()
if err != nil {
logger.Warn("Could not determine local IP address: ", err.Error())
} else {
......@@ -114,13 +110,7 @@ func configure() error {
err := viper.ReadInConfig() // Hold error checking until we know how much of it to log
// Set log level
verbosity := viper.GetInt("verbose")
if verbosity == 1 {
logger.Level = logrus.DebugLevel
}
if verbosity >= 2 {
logger.Level = logrus.TraceLevel
}
logger.Level = server.Verbosity(viper.GetInt("verbose"))
if viper.GetBool("quiet") {
logger.Out = ioutil.Discard
}
......@@ -156,9 +146,6 @@ func configure() error {
Verbose: viper.GetInt("verbose"),
Quiet: viper.GetBool("quiet"),
}
// replace "port" in url with actual port
replace := "$1:" + strconv.Itoa(conf.Port)
conf.URL = string(regexp.MustCompile("(https?://[^/]*):port").ReplaceAll([]byte(conf.URL), []byte(replace)))
// Handle global permissions
if len(viper.GetStringMap("permissions")) > 0 { // First read config file
......@@ -203,40 +190,3 @@ func handlePermission(conf []string, typ string) []string {
}
return perms
}
func localIP() (string, error) {
ifaces, err := net.Interfaces()
if err != nil {
return "", err
}
for _, iface := range ifaces {
if iface.Flags&net.FlagUp == 0 {
continue // interface down
}
if iface.Flags&net.FlagLoopback != 0 {
continue // loopback interface
}
addrs, err := iface.Addrs()
if err != nil {
return "", err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip == nil || ip.IsLoopback() {
continue
}
ip = ip.To4()
if ip == nil {
continue // not an ipv4 address
}
return ip.String(), nil
}
}
return "", errors.New("No IP found")
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment