|
1 | | -import client.Client; |
2 | 1 | import crypto.Crypto; |
| 2 | +import server.LocalServer; |
3 | 3 | import org.slf4j.Logger; |
4 | 4 | import org.slf4j.LoggerFactory; |
5 | | -import server.Server; |
6 | | -import util.LocalConfig; |
7 | | -import util.Mode; |
8 | | -import util.ServerConfig; |
9 | | -import util.Util; |
| 5 | +import server.RemoteServer; |
| 6 | +import util.*; |
10 | 7 |
|
11 | 8 | public class Main { |
12 | 9 |
|
13 | 10 | private final static Logger logger = LoggerFactory.getLogger(Main.class); |
14 | 11 |
|
15 | | - private static void printHelpInfo() { |
16 | | - System.out.println("Usage: java -jar lightsocks.jar -c config.json --client | --server"); |
| 12 | + |
| 13 | + public static void checkCrypto(Crypto crypto) { |
| 14 | + if(crypto == null) { |
| 15 | + logger.error("unknown crypto"); |
| 16 | + System.exit(1); |
| 17 | + } |
17 | 18 | } |
18 | 19 |
|
19 | 20 | public static void main(String[] args) { |
20 | | - logger.info("starting."); |
| 21 | + logger.info("starting..."); |
21 | 22 | String configPath = null; |
22 | 23 | Mode mode = Mode.Unknown; |
23 | 24 |
|
24 | | - for(int i=0;i<args.length;i++) { |
25 | | - if(args[i].equals("-c") && i + 1 < args.length) { |
| 25 | + |
| 26 | + // load config path |
| 27 | + for (int i = 0; i < args.length; i++) { |
| 28 | + if (args[i].equals("-c") && i + 1 < args.length) { |
26 | 29 | configPath = args[i + 1]; |
27 | 30 | i++; |
28 | | - } else if(args[i].equals("-h")) { |
29 | | - printHelpInfo(); |
| 31 | + } else if (args[i].equals("-h")) { |
| 32 | + Util.printHelpInfo(); |
30 | 33 | System.exit(0); |
31 | 34 | } |
32 | 35 | } |
33 | 36 |
|
| 37 | + // decide running mode |
| 38 | + mode = ConfigLoader.loadConfigMode(configPath); |
| 39 | + if (mode == Mode.Local) { |
| 40 | + LocalConfig config = ConfigLoader.loadAsLocalConfig(configPath); |
34 | 41 |
|
35 | | - try { |
36 | | - if(Util.getJsonObjectFromFile(configPath).get("mode").getAsString().equalsIgnoreCase("server")) { |
37 | | - mode = Mode.Server; |
38 | | - } else if (Util.getJsonObjectFromFile(configPath).get("mode").getAsString().equalsIgnoreCase("local")) { |
39 | | - mode = Mode.Client; |
40 | | - } |
41 | | - } catch (Exception e) { |
42 | | - logger.error("", e); |
43 | | - System.exit(1); |
44 | | - } |
45 | | - |
46 | | - |
47 | | - if(mode == Mode.Client) { |
48 | | - LocalConfig localConfig = LocalConfig.loadConfigFromFile(configPath); |
49 | | - if(localConfig == null) { |
50 | | - logger.info("error: failed to load local config, exit."); |
| 42 | + if (config == null) { |
| 43 | + logger.info("failed to load local config."); |
51 | 44 | System.exit(1); |
52 | 45 | } else { |
53 | | - logger.info(localConfig.toString()); |
| 46 | + logger.info(config.toString()); |
54 | 47 | } |
55 | 48 |
|
56 | | - Client local = new Client(localConfig.getHost(),localConfig.getHostPort(),localConfig.getLocalPort()); |
57 | | - Crypto crypto = localConfig.getCrypto(); |
58 | | - if(crypto==null) { |
59 | | - logger.info("error: failed to init cipher,exit."); |
60 | | - System.exit(1); |
61 | | - } else { |
62 | | - Client.crypto = crypto; |
63 | | - } |
64 | | - local.listen(); |
| 49 | + checkCrypto(config.getCrypto()); |
| 50 | + |
| 51 | + LocalServer localServer = new LocalServer(config.getHost(), config.getHostPort(), config.getLocalPort()); |
| 52 | + LocalServer.crypto = config.getCrypto(); |
| 53 | + localServer.listen(); |
65 | 54 |
|
66 | 55 | } else if (mode == Mode.Server) { |
| 56 | + ServerConfig config = ConfigLoader.loadAsServerConfig(configPath); |
67 | 57 |
|
68 | | - ServerConfig serverConfig = ServerConfig.loadConfigFromFile(configPath); |
69 | | - if(serverConfig == null) { |
70 | | - logger.info("error: failed to load server config, exit."); |
| 58 | + if (config == null) { |
| 59 | + logger.info("failed to load server config."); |
71 | 60 | System.exit(1); |
72 | 61 | } else { |
73 | | - logger.info(serverConfig.toString()); |
| 62 | + logger.info(config.toString()); |
74 | 63 | } |
75 | 64 |
|
76 | | - Server server = new Server(serverConfig.getPort()); |
77 | | - Crypto crypto = serverConfig.getCrypto(); |
78 | | - if(crypto==null) { |
79 | | - logger.info("error: failed to init cipher,exit."); |
80 | | - System.exit(1); |
81 | | - } else { |
82 | | - Server.crypto = crypto; |
83 | | - } |
84 | | - server.listen(); |
| 65 | + checkCrypto(config.getCrypto()); |
| 66 | + |
| 67 | + RemoteServer remoteServer = new RemoteServer(config.getPort()); |
| 68 | + RemoteServer.crypto = config.getCrypto(); |
| 69 | + remoteServer.listen(); |
| 70 | + |
| 71 | + } else { |
| 72 | + logger.error("unknown running mode"); |
| 73 | + System.exit(1); |
85 | 74 | } |
86 | 75 |
|
87 | 76 |
|
|
0 commit comments