11package com .netiq .websockify ;
22
3- import org .jboss .netty .bootstrap .ServerBootstrap ;
4- import org .jboss .netty .channel .Channel ;
5- import org .jboss .netty .channel .socket .ClientSocketChannelFactory ;
6- import org .jboss .netty .channel .socket .nio .NioClientSocketChannelFactory ;
7- import org .jboss .netty .channel .socket .nio .NioServerSocketChannelFactory ;
8-
9- import java .net .InetSocketAddress ;
10- import java .util .concurrent .Executor ;
11- import java .util .concurrent .Executors ;
3+ import org .kohsuke .args4j .CmdLineException ;
4+ import org .kohsuke .args4j .CmdLineParser ;
5+ import org .kohsuke .args4j .Option ;
126
137public class Websockify {
14- private Executor executor ;
15- private ServerBootstrap sb ;
16- private ClientSocketChannelFactory cf ;
17- private Channel serverChannel = null ;
188
19- public Websockify ( )
20- {
21- // Configure the bootstrap.
22- executor = Executors .newCachedThreadPool ();
23- sb = new ServerBootstrap (new NioServerSocketChannelFactory (executor , executor ));
24-
25- // Set up the event pipeline factory.
26- cf = new NioClientSocketChannelFactory (executor , executor );
27- }
9+ @ Option (name ="--help" ,usage ="show this help message" )
10+ private boolean showHelp = false ;
2811
29- public void connect ( int localPort , String remoteHost , int remotePort , boolean useSSL , boolean enableWebServer )
30- {
31- connect ( localPort , new StaticTargetResolver ( remoteHost , remotePort ), useSSL , enableWebServer );
32- }
12+ @ Option (name ="--port" ,usage ="(required) local port the websockify server will listen on" ,required =true )
13+ private int port ;
3314
34- public void connect ( int localPort , IProxyTargetResolver resolver , boolean useSSL , boolean enableWebServer )
35- {
36- if ( serverChannel != null )
37- {
38- close ( );
39- }
40-
41- sb .setPipelineFactory (new WebsockifyProxyPipelineFactory (cf , resolver , useSSL , enableWebServer ));
42-
43- // Start up the server.
44- serverChannel = sb .bind (new InetSocketAddress (localPort ));
45-
46- }
15+ @ Option (name ="--remote-host" ,usage ="(required) remote host the websockify server will proxy to" ,required =true )
16+ private String remoteHost ;
4717
48- public void close ( )
49- {
50- if ( serverChannel != null && serverChannel .isBound () )
51- {
52- serverChannel .close ();
53- serverChannel = null ;
54- }
55- }
18+ @ Option (name ="--remote-port" ,usage ="(required) remote port the websockify server will proxy to" ,required =true )
19+ private int remotePort ;
5620
57- public Channel getChannel ( )
58- {
59- return serverChannel ;
60- }
21+ @ Option (name ="--enable-ssl" ,usage ="enable SSL" )
22+ private boolean enableSSL = false ;
6123
6224 public static void main (String [] args ) throws Exception {
63- // Validate command line options.
64- if (args .length != 3 && args .length != 4 ) {
65- System .err .println (
66- "Usage: " + Websockify .class .getSimpleName () +
67- " <local port> <remote host> <remote port> [encrypt]" );
68- return ;
69- }
25+ new Websockify ().doMain (args );
26+ }
27+
28+ public void doMain (String [] args ) throws Exception {
29+ CmdLineParser parser = new CmdLineParser (this );
30+ parser .setUsageWidth (80 );
31+
32+ try {
33+ parser .parseArgument (args );
34+ }
35+ catch (CmdLineException e ) {
36+ System .err .println (e .getMessage ());
37+ System .err .println ("java -jar websockify.jar [options...]" );
38+ parser .printUsage (System .err );
39+ return ;
40+ }
41+
42+ if ( showHelp ) {
43+ System .err .println ("java -jar websockify.jar [options...]" );
44+ parser .printUsage (System .out );
45+ return ;
46+ }
7047
71- if (args . length == 4 ) {
48+ if (enableSSL ) {
7249 String keyStoreFilePath = System .getProperty ("keystore.file.path" );
7350 if (keyStoreFilePath == null || keyStoreFilePath .isEmpty ()) {
7451 System .out .println ("ERROR: System property keystore.file.path not set. Exiting now!" );
@@ -82,19 +59,13 @@ public static void main(String[] args) throws Exception {
8259 }
8360 }
8461
85- // Parse command line options.
86- int localPort = Integer .parseInt (args [0 ]);
87- String remoteHost = args [1 ];
88- int remotePort = Integer .parseInt (args [2 ]);
89- boolean useSSL = args .length < 4 ? false : true ;
90-
9162 System .out .println (
92- "Websockify Proxying *:" + localPort + " to " +
63+ "Websockify Proxying *:" + port + " to " +
9364 remoteHost + ':' + remotePort + " ..." );
94- if (useSSL ) System .out .println ("Websocket communications are SSL encrypted." );
65+ if (enableSSL ) System .out .println ("Websocket communications are SSL encrypted." );
9566
96- Websockify ws = new Websockify ( );
97- ws .connect ( localPort , remoteHost , remotePort , useSSL , true );
67+ WebsockifyServer wss = new WebsockifyServer ( );
68+ wss .connect ( port , remoteHost , remotePort , enableSSL , true );
9869
9970 }
10071
0 commit comments