Blame view

go/src/themaru/ip.go 1.06 KB
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
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
  package main
  
  import (
  	"errors"
  	"regexp"
  	"strconv"
  )
  
  func getIpAddress() string {
  	env, err := getEnv(0)
  	if err != nil {
  		return ""
  	}
  
  	return env["ipaddr"]
  }
  
  func getIpAddress2() string {
  	env, err := getEnv(0)
  	if err != nil {
  		return ""
  	}
  
  	return env["ipaddr2"]
  }
  
  func setIpAddress(address, address2 string) error {
  	address = extractIpAddress(address)
  	if address == "" {
  		return errors.New("invalid IP address")
  	}
  
  	address2 = extractIpAddress(address2)
  	if address2 == "" {
  		return errors.New("invalid IP address2")
  	}
  
  	env, err := getEnv(0)
  	if err != nil {
  		return err
  	}
  
  	env["ipaddr"] = address
  	env["ipaddr2"] = address2
  
  	err = setEnv(0, env)
  	if err != nil {
  		return err
  	}
  
  	return nil
  }
  
  func extractIpAddress(address string) string {
  	r := regexp.MustCompile(`([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)`)
  	s := r.FindStringSubmatch(address)
  	if len(s) != 5 {
  		return ""
  	}
  
  	for i, v := range s {
  		if i == 0 {
  			continue
  		}
  
  		u, err := strconv.ParseUint(v, 10, 8)
  		if err != nil {
  			return ""
  		}
  
  		if u > 255 {
  			return ""
  		}
  	}
  
  	return s[0]
  }