Class: Config

Inherits:
Object
  • Object
show all
Defined in:
lib/ssport/config.rb

Constant Summary collapse

@@SERVER_PORT =
"server_port"
@@PASSWORD =
"password"
@@METHOD =
"method"

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Config

Returns a new instance of Config.



10
11
12
13
# File 'lib/ssport/config.rb', line 10

def initialize(options)
    @options = options
    @config_file = options[:config]
end

Instance Method Details

#applyChangeObject



58
59
60
61
62
63
64
65
66
# File 'lib/ssport/config.rb', line 58

def applyChange
    changeField @@SERVER_PORT , :port
    changeField @@PASSWORD , :password
    changeField @@METHOD , :method
    final_config = JSON.pretty_generate(@config_json)
    puts "-------------New Config---------------".colorize(:yellow)
    puts final_config
    File.write @config_file , final_config 
end

#changeField(config_key, option_key) ⇒ Object



51
52
53
54
55
56
# File 'lib/ssport/config.rb', line 51

def changeField(config_key, option_key)
    option_value = @options[option_key]
    if option_value 
        @config_json[config_key] = option_value
    end
end

#createObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ssport/config.rb', line 15

def create 
   defualt_config = {
    "server": @options[:server] || "my_server_ip",
    "server_port": 8388,
    "local_address":"127.0.0.1",
    "local_port": 1080,
    "password": "123456",
    "timeout": 300,
    "method": "rc4-md5"
    }
    File.write '/etc/shadowsocks.json' , defualt_config 

    parseConfig
    applyChange
end

#parseConfigObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ssport/config.rb', line 39

def parseConfig
    puts "---------Bengin Deal #{@config_file} ------------".colorize(:yellow)
    if File.exist? @config_file 
        file_content = File.read @config_file
        @config_json = JSON.parse file_content
        puts "-------------Old Config---------------".colorize(:yellow)
        puts JSON.pretty_generate(@config_json)
    else 
        puts "Config 文件不存在".colorize(:red)
    end
end

#updateObject



31
32
33
34
35
36
37
# File 'lib/ssport/config.rb', line 31

def update
    if !@config_file 
        raise "config file not set"
    end 
    parseConfig
    applyChange
end