Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
new config tool
Browse files Browse the repository at this point in the history
  • Loading branch information
lodevil committed Mar 16, 2013
1 parent b455df6 commit cb87360
Show file tree
Hide file tree
Showing 10 changed files with 412 additions and 223 deletions.
61 changes: 25 additions & 36 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,56 +5,58 @@ import (
"log"
)

type authConfig struct {
Username string
Password string
}

type Client struct {
cfg map[string]map[string]interface{}
cfg Config
tunnel ClientTunnel
cli_ch ClientChan
nat_info NatInfo
}

func NewClient(cfg map[string]map[string]interface{}) (cli Client, err error) {
var name interface{}
auth_cfg authConfig
tunnel_cfg Config
}

func NewClient(cfg Config) (cli Client, err error) {
cli.cfg = cfg
if err = InitPacket(cfg); err != nil {
if pkg_cfg, e := cfg.GetConfig("packet"); e != nil {
err = e
return
} else if err = InitPacket(pkg_cfg); err != nil {
return
}

if _, ok := cfg["auth"]; !ok {
err = fmt.Errorf("missing `auth`")
if err = cfg.Get("auth", &cli.auth_cfg); err != nil {
return
}

tunnel_cfg, ok := cfg["tunnel"]
if !ok {
err = fmt.Errorf("missing `tunnel`")
var tunnel_name string
if cli.tunnel_cfg, err = cfg.GetConfig("tunnel"); err != nil {
return
}
name, ok = tunnel_cfg["name"]
if !ok {
err = fmt.Errorf("missing `tunnel.name`")
} else if err = cli.tunnel_cfg.Get("name", &tunnel_name); err != nil {
return
} else {
if _, ok = name.(string); !ok {
err = fmt.Errorf("tunnel.name is not a string")
return
}
}

if cli.tunnel, err = NewClientTunnel(name.(string)); err != nil {
if cli.tunnel, err = NewClientTunnel(tunnel_name); err != nil {
return
}

cli.cli_ch = NewClientChan()

return
}

func (c *Client) Init() error {
return c.tunnel.Init(c.cfg["tunnel"])
return c.tunnel.Init(c.tunnel_cfg)
}

func (c *Client) Run() error {
defer c.cli_ch.Close()

log.Println("client running")

if err := c.tunnel.Start(c.cli_ch); err != nil {
return err
}
Expand All @@ -70,22 +72,9 @@ func (c *Client) Shutdown() error {
}

func (c *Client) auth() error {
var user, passwd interface{}
var ok bool
var rst AuthResult

if user, ok = c.cfg["auth"]["username"]; !ok {
return fmt.Errorf("missing `username`")
} else if _, ok = user.(string); !ok {
return fmt.Errorf("invalid username type (string desired)")
}
if passwd, ok = c.cfg["auth"]["password"]; !ok {
return fmt.Errorf("missing `password`")
} else if _, ok = passwd.(string); !ok {
return fmt.Errorf("invalid password type (string desired)")
}

p := NewPacket(PT_AUTH, &AuthInfo{user.(string), passwd.(string)})
p := NewPacket(PT_AUTH, &AuthInfo{c.auth_cfg.Username, c.auth_cfg.Password})
c.cli_ch.W <- p
p = <-c.cli_ch.R
if p.Decode(&rst) != nil {
Expand Down
14 changes: 2 additions & 12 deletions cmd/client.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package main

import (
json "encoding/json"
"flag"
"log"
"os"
"secretun"
)

var cfgfile = flag.String("cfg", "cli.cfg", "configure file path")

func main() {
flag.Parse()
var cfg = map[string]map[string]interface{}{}

if f, err := os.Open(*cfgfile); err != nil {
cfg, err := secretun.ConfigFromJson(*cfgfile)
if err != nil {
log.Println(err)
return
} else {
defer f.Close()
decoder := json.NewDecoder(f)
if err := decoder.Decode(&cfg); err != nil {
log.Println(err)
return
}
}

if cli, err := secretun.NewClient(cfg); err != nil {
Expand Down
14 changes: 2 additions & 12 deletions cmd/server.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,19 @@
package main

import (
json "encoding/json"
"flag"
"log"
"os"
"secretun"
)

var cfgfile = flag.String("cfg", "ser.cfg", "configure file path")

func main() {
flag.Parse()
var cfg = map[string]map[string]interface{}{}

if f, err := os.Open(*cfgfile); err != nil {
cfg, err := secretun.ConfigFromJson(*cfgfile)
if err != nil {
log.Println(err)
return
} else {
defer f.Close()
decoder := json.NewDecoder(f)
if err := decoder.Decode(&cfg); err != nil {
log.Println(err)
return
}
}

if ser, err := secretun.NewServer(cfg); err != nil {
Expand Down
27 changes: 6 additions & 21 deletions compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,20 @@ package secretun
import (
"bytes"
zlib "compress/zlib"
"fmt"
"io"
)

type ZlibEncoder struct {
level int
}

func (z *ZlibEncoder) Init(cfg map[string]interface{}) error {
if ilevel, ok := cfg["level"]; !ok {
z.level = 6
} else {
var level int
switch v := ilevel.(type) {
case int:
level = v
case int64:
level = int(v)
case float32:
level = int(v)
case float64:
level = int(v)
default:
return fmt.Errorf("zlib.level invalid type (int desired) %t", ilevel)
}
if level < zlib.NoCompression || level > zlib.BestCompression {
return fmt.Errorf("zlib.level invalid")
func (z *ZlibEncoder) Init(cfg Config) error {
if err := cfg.Get("level", &z.level); err != nil {
if err.(*ConfigError).Errno == ErrMissing {
z.level = 6
} else {
return err
}
z.level = level
}

return nil
Expand Down
Loading

0 comments on commit cb87360

Please sign in to comment.