├── README.md ├── .gitignore ├── portforward ├── nosuppression.xml ├── src │ └── main │ │ └── java │ │ └── com │ │ └── sharneng │ │ ├── io │ │ ├── package-info.java │ │ └── IOUtils.java │ │ ├── util │ │ ├── package-info.java │ │ └── NonnullByDefault.java │ │ └── net │ │ ├── package-info.java │ │ ├── portforward │ │ ├── package-info.java │ │ ├── Cleanable.java │ │ ├── TargetConnector.java │ │ ├── Forwarder.java │ │ ├── Cleaner.java │ │ ├── Processor.java │ │ └── Listener.java │ │ ├── misc │ │ ├── package-info.java │ │ ├── EchoProcessor.java │ │ └── EchoServer.java │ │ └── NetUtils.java ├── checkstyle-config.xml └── pom.xml └── LICENSE.txt /README.md: -------------------------------------------------------------------------------- 1 | lookup 2 | ====== 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | # Package Files # 4 | *.jar 5 | *.war 6 | *.ear 7 | 8 | # Eclipse Files # 9 | *.classpath 10 | *.project 11 | *.settings 12 | target 13 | -------------------------------------------------------------------------------- /portforward/nosuppression.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/io/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Original Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * General purpose utilities. 18 | * 19 | * @author Kenneth Xu 20 | */ 21 | @com.sharneng.util.NonnullByDefault 22 | package com.sharneng.io; 23 | 24 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/util/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Original Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Classes related to IO operations. 18 | * 19 | * @author Kenneth Xu 20 | */ 21 | @com.sharneng.util.NonnullByDefault 22 | package com.sharneng.util; 23 | 24 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Original Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Classes related to network operations. 18 | * 19 | * @author Kenneth Xu 20 | */ 21 | @com.sharneng.util.NonnullByDefault 22 | package com.sharneng.net; 23 | 24 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/portforward/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Original Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Classes to implement the TCP/IP port forwarding. 18 | * 19 | * @author Kenneth Xu 20 | */ 21 | @com.sharneng.util.NonnullByDefault 22 | package com.sharneng.net.portforward; 23 | 24 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/misc/package-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 Original Authors 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | /** 17 | * Classes to implement an echo network server to test the connectivity. 18 | * 19 | * @author Kenneth Xu 20 | */ 21 | @com.sharneng.util.NonnullByDefault 22 | package com.sharneng.net.misc; 23 | 24 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/portforward/Cleanable.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */package com.sharneng.net.portforward; 16 | 17 | /** 18 | * 19 | * @author Kenneth Xu 20 | * 21 | */ 22 | interface Cleanable { 23 | boolean isCompleted(); 24 | 25 | void close(); 26 | } 27 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/util/NonnullByDefault.java: -------------------------------------------------------------------------------- 1 | package com.sharneng.util; 2 | 3 | import java.lang.annotation.Documented; 4 | import java.lang.annotation.ElementType; 5 | import java.lang.annotation.Retention; 6 | import java.lang.annotation.RetentionPolicy; 7 | 8 | import javax.annotation.Nonnull; 9 | import javax.annotation.meta.TypeQualifierDefault; 10 | 11 | /** 12 | * Default annotation to set package-level {@link javax.annotation.Nonnull Nonnull} for null check analysis. Apply this 13 | * annotation to package definition makes every field, parameter and method return value of any classes in the package 14 | * {@link javax.annotation.Nonnull Nonnull} unless otherwise annotated with {@link javax.annotation.CheckForNull 15 | * CheckForNull}. 16 | */ 17 | @Documented 18 | @Nonnull 19 | @TypeQualifierDefault({ ElementType.PARAMETER, ElementType.FIELD, ElementType.METHOD }) 20 | @Retention(RetentionPolicy.RUNTIME) 21 | public @interface NonnullByDefault { 22 | } 23 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/portforward/TargetConnector.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.sharneng.net.portforward; 17 | 18 | import org.apache.commons.logging.Log; 19 | import org.apache.commons.logging.LogFactory; 20 | 21 | import java.net.InetSocketAddress; 22 | import java.net.Socket; 23 | 24 | /** 25 | * 26 | * @author Kenneth Xu 27 | * 28 | */ 29 | class TargetConnector { 30 | private static Log log = LogFactory.getLog(TargetConnector.class); 31 | private final InetSocketAddress to; 32 | 33 | public TargetConnector(InetSocketAddress to) { 34 | log.trace("new ServerConnectionManager( InetSocketAddress )"); 35 | this.to = to; 36 | } 37 | 38 | public Socket openSocket() throws java.io.IOException { 39 | return new Socket(to.getAddress(), to.getPort()); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/portforward/Forwarder.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.sharneng.net.portforward; 17 | 18 | import com.sharneng.net.NetUtils; 19 | 20 | /** 21 | * The command line entry for TCP/IP port forwarding. 22 | * 23 | * @author Kenneth Xu 24 | * 25 | */ 26 | public final class Forwarder { 27 | 28 | private Forwarder() { 29 | } 30 | 31 | /** 32 | * The command line entry. 33 | * 34 | * @param args 35 | * command line arguments 36 | * @throws Exception 37 | * when error occurs 38 | */ 39 | public static void main(String[] args) throws Exception { 40 | Listener listener = new Listener(NetUtils.parseInetSocketAddress(args[0]), 41 | NetUtils.parseInetSocketAddress(args[1])); 42 | listener.run(); 43 | listener.close(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/portforward/Cleaner.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.sharneng.net.portforward; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | import java.util.Iterator; 21 | 22 | import org.apache.commons.logging.Log; 23 | import org.apache.commons.logging.LogFactory; 24 | 25 | /** 26 | * 27 | * @author Kenneth Xu 28 | * 29 | */ 30 | class Cleaner implements Runnable { 31 | private static final int CLEAN_INTERVAL = 3000; 32 | 33 | private static final Log log = LogFactory.getLog(Cleaner.class); 34 | 35 | private final List list = new ArrayList(); 36 | 37 | @Override 38 | public void run() { 39 | while (true) { 40 | cleanup(); 41 | try { 42 | wait(CLEAN_INTERVAL); 43 | } catch (InterruptedException e) { 44 | log.error(e.getMessage(), e); 45 | } 46 | } 47 | } 48 | 49 | private synchronized void cleanup() { 50 | for (Iterator itr = list.iterator(); itr.hasNext();) { 51 | Cleanable p = itr.next(); 52 | if (p.isCompleted()) { 53 | p.close(); 54 | itr.remove(); 55 | } 56 | } 57 | } 58 | 59 | public synchronized void add(Cleanable p) { 60 | list.add(p); 61 | } 62 | 63 | } 64 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/misc/EchoProcessor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.sharneng.net.misc; 17 | 18 | import com.sharneng.io.IOUtils; 19 | import com.sharneng.net.NetUtils; 20 | 21 | import java.io.IOException; 22 | import java.net.Socket; 23 | 24 | import org.apache.commons.logging.Log; 25 | import org.apache.commons.logging.LogFactory; 26 | 27 | class EchoProcessor implements Runnable { 28 | private static final Log log = LogFactory.getLog(EchoProcessor.class); 29 | 30 | private final boolean isCompleted = false; 31 | 32 | private final Socket socket; 33 | 34 | public EchoProcessor(Socket source) { 35 | this.socket = source; 36 | } 37 | 38 | public void process() { 39 | Thread t = new Thread(this); 40 | t.start(); 41 | 42 | if (log.isTraceEnabled()) { 43 | log.trace("started new process threads: " + this); 44 | } 45 | } 46 | 47 | public boolean isCompleted() { 48 | return isCompleted; 49 | } 50 | 51 | public void close() { 52 | if (log.isTraceEnabled()) log.trace("close() called on " + this); 53 | if (socket.isClosed()) { 54 | if (log.isTraceEnabled()) log.trace("socket already closed: " + socket); 55 | } else { 56 | if (log.isTraceEnabled()) log.trace("closing socket: " + socket); 57 | NetUtils.close(socket); 58 | } 59 | } 60 | 61 | @Override 62 | public String toString() { 63 | return this.getClass().getName() + "(" + socket + ")"; 64 | } 65 | 66 | @Override 67 | public void run() { 68 | try { 69 | IOUtils.copyStream(socket.getInputStream(), socket.getOutputStream(), false); 70 | } catch (IOException e) { 71 | log.error(e.getMessage(), e); 72 | } finally { 73 | close(); 74 | } 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/misc/EchoServer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.sharneng.net.misc; 17 | 18 | import com.sharneng.net.NetUtils; 19 | 20 | import org.apache.commons.logging.Log; 21 | import org.apache.commons.logging.LogFactory; 22 | 23 | import java.io.IOException; 24 | import java.net.InetSocketAddress; 25 | import java.net.ServerSocket; 26 | import java.net.Socket; 27 | 28 | import javax.annotation.CheckForNull; 29 | 30 | class EchoServer implements Runnable { 31 | private static Log log = LogFactory.getLog(EchoServer.class); 32 | private final ServerSocket serverSocket; 33 | private final InetSocketAddress from; 34 | @CheckForNull 35 | private Throwable exception; 36 | 37 | // private Cleaner cleaner = new Cleaner(); 38 | 39 | public EchoServer(InetSocketAddress from) throws IOException { 40 | this.from = from; 41 | serverSocket = new ServerSocket(); 42 | serverSocket.setReuseAddress(true); 43 | serverSocket.bind(from); 44 | String hostname = from.getHostName(); 45 | if (hostname == null) hostname = "*"; 46 | log.info("Ready to accept client connection on " + hostname + ":" + from.getPort()); 47 | } 48 | 49 | @Override 50 | public void run() { 51 | Socket source = null; 52 | // new Thread(cleaner).start(); 53 | while (true) { 54 | try { 55 | source = serverSocket.accept(); 56 | log.trace("accepted client connection"); 57 | new EchoProcessor(source).process(); 58 | } catch (IOException e) { 59 | String msg = "Failed to accept client connection on port " + from.getPort(); 60 | log.error(msg, e); 61 | exception = e; 62 | return; 63 | } 64 | } 65 | } 66 | 67 | public void close() { 68 | if (!serverSocket.isClosed()) { 69 | try { 70 | serverSocket.close(); 71 | } catch (IOException e) { 72 | log.error(e.getMessage(), e); 73 | } 74 | } 75 | } 76 | 77 | public static void main(String[] args) throws Throwable { 78 | EchoServer server = new EchoServer(NetUtils.parseInetSocketAddress(args[0])); 79 | server.run(); 80 | if (server.exception != null) throw server.exception; 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/portforward/Processor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2007 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.sharneng.net.portforward; 18 | 19 | import com.sharneng.io.IOUtils; 20 | import com.sharneng.net.NetUtils; 21 | 22 | import java.net.Socket; 23 | 24 | import org.apache.commons.logging.Log; 25 | import org.apache.commons.logging.LogFactory; 26 | 27 | /** 28 | * 29 | * @author Kenneth Xu 30 | * 31 | */ 32 | class Processor implements Cleanable { 33 | private static final Log log = LogFactory.getLog(Processor.class); 34 | 35 | private final Socket source; 36 | private final Socket target; 37 | private final Cleaner cleaner; 38 | 39 | private final Copier req; 40 | private final Copier res; 41 | 42 | public Processor(Socket source, Socket target, Cleaner cleaner) { 43 | this.source = source; 44 | this.target = target; 45 | this.cleaner = cleaner; 46 | req = new Copier(source, target); 47 | res = new Copier(target, source); 48 | cleaner.add(this); 49 | } 50 | 51 | public void process() { 52 | 53 | new Thread(req).start(); 54 | new Thread(res).start(); 55 | if (log.isTraceEnabled()) { 56 | log.trace("started new request stream copier threads: " + req); 57 | log.trace("started new response stream copier threads: " + res); 58 | } 59 | } 60 | 61 | @Override 62 | public boolean isCompleted() { 63 | return req.isCompleted && res.isCompleted; 64 | } 65 | 66 | @Override 67 | public void close() { 68 | if (log.isTraceEnabled()) log.trace("close() called on " + this); 69 | NetUtils.close(source); 70 | NetUtils.close(target); 71 | } 72 | 73 | @Override 74 | public String toString() { 75 | return this.getClass().getName() + "(from " + source + " to " + target + ")"; 76 | } 77 | 78 | private class Copier implements Runnable { 79 | private final Socket in; 80 | private final Socket out; 81 | private boolean isCompleted = false; 82 | 83 | Copier(Socket in, Socket out) { 84 | this.in = in; 85 | this.out = out; 86 | } 87 | 88 | @Override 89 | public void run() { 90 | try { 91 | IOUtils.copyStream(in.getInputStream(), out.getOutputStream(), false); 92 | } catch (java.net.SocketException e) { 93 | NetUtils.shutdown(source); 94 | NetUtils.shutdown(target); 95 | log.debug(this, e); 96 | } catch (Exception e) { 97 | log.error(this, e); 98 | } finally { 99 | this.isCompleted = true; 100 | if (cleaner != null) synchronized (cleaner) { 101 | cleaner.notify(); 102 | } 103 | } 104 | } 105 | 106 | @Override 107 | public String toString() { 108 | return this.getClass().getName() + "(from " + in + " to " + out + ")"; 109 | } 110 | 111 | } 112 | } 113 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/portforward/Listener.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.sharneng.net.portforward; 17 | 18 | import org.apache.commons.logging.Log; 19 | import org.apache.commons.logging.LogFactory; 20 | 21 | import java.io.Closeable; 22 | import java.io.IOException; 23 | import java.net.InetSocketAddress; 24 | import java.net.ServerSocket; 25 | import java.net.Socket; 26 | 27 | import javax.annotation.CheckForNull; 28 | 29 | /** 30 | * Listens on the given port to accept connection and forward it to target. 31 | * 32 | * @author Kenneth Xu 33 | * 34 | */ 35 | public class Listener implements Runnable, Closeable { 36 | private static Log log = LogFactory.getLog(Listener.class); 37 | private final ServerSocket serverSocket; 38 | private final InetSocketAddress from, to; 39 | @CheckForNull 40 | private Throwable exception; 41 | private final Cleaner cleaner = new Cleaner(); 42 | 43 | /** 44 | * Gets the exception occurred if any. 45 | * 46 | * @return the exception if any error occurred 47 | */ 48 | @CheckForNull 49 | public Throwable getException() { 50 | return exception; 51 | } 52 | 53 | /** 54 | * Constructs a new instance of Listener. 55 | * 56 | * @param from 57 | * the address to listen for connections 58 | * @param to 59 | * the address to forward connections to 60 | * @throws IOException 61 | * when something is not going write when network operation 62 | */ 63 | public Listener(InetSocketAddress from, InetSocketAddress to) throws IOException { 64 | this.from = from; 65 | this.to = to; 66 | serverSocket = new ServerSocket(); 67 | serverSocket.setReuseAddress(true); 68 | serverSocket.bind(from); 69 | String hostname = from.getHostName(); 70 | if (hostname == null) hostname = "*"; 71 | log.info("Ready to accept client connection on " + hostname + ":" + from.getPort()); 72 | } 73 | 74 | /** 75 | * {@inheritDoc} 76 | */ 77 | @Override 78 | public void run() { 79 | Socket source = null; 80 | new Thread(cleaner).start(); 81 | while (true) { 82 | try { 83 | TargetConnector connector = new TargetConnector(to); 84 | source = serverSocket.accept(); 85 | log.trace("accepted client connection"); 86 | Socket target = connector.openSocket(); 87 | new Processor(source, target, cleaner).process(); 88 | } catch (IOException e) { 89 | String msg = "Failed to accept client connection on port " + from.getPort(); 90 | log.error(msg, e); 91 | exception = e; 92 | return; 93 | } 94 | } 95 | } 96 | 97 | /** 98 | * Closes this listener and associated resources. 99 | */ 100 | @Override 101 | public void close() { 102 | if (!serverSocket.isClosed()) { 103 | try { 104 | serverSocket.close(); 105 | } catch (IOException e) { 106 | log.error(e.getMessage(), e); 107 | } 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/net/NetUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.sharneng.net; 17 | 18 | import java.net.InetSocketAddress; 19 | import java.net.Socket; 20 | 21 | import org.apache.commons.logging.Log; 22 | import org.apache.commons.logging.LogFactory; 23 | 24 | /** 25 | * Networking related static utilties methods are defined in this class. 26 | * 27 | * @author Kenneth Xu 28 | * 29 | */ 30 | public final class NetUtils { 31 | private static final Log log = LogFactory.getLog(NetUtils.class); 32 | 33 | private NetUtils() { 34 | } 35 | 36 | /** 37 | * Parse an {@link InetSocketAddress} object the given string. The endPoint is in format of 38 | * [hostname:]port. It can be either a port number, or a hostname and port number separated by the 39 | * character ':'. 40 | *

41 | * 42 | * For example, 'www.company.com:80', '1234' and ' localhost:3344' are all 43 | * valid end points. 44 | * 45 | * @param endPoint 46 | * the string to parse. 47 | * @return the {@linkplain InetSocketAddress} parsed from string. 48 | * @exception NumberFormatException 49 | * if the port number is not a integer. 50 | */ 51 | public static InetSocketAddress parseInetSocketAddress(String endPoint) { 52 | String hostname = null; 53 | String portString; 54 | int port; 55 | 56 | int index = endPoint.indexOf(":"); 57 | if (index >= 0) { 58 | hostname = endPoint.substring(0, index); 59 | portString = endPoint.substring(index + 1); 60 | } else { 61 | portString = endPoint; 62 | } 63 | port = Integer.parseInt(portString); 64 | return hostname == null ? new InetSocketAddress(port) : new InetSocketAddress(hostname, port); 65 | } 66 | 67 | /** 68 | * Quietly close a {@linkplain Socket} object. It tries to shutdown the input and output of the socket before close. 69 | * Exceptions are ignored. 70 | * 71 | * @param socket 72 | * the Socket object to close 73 | * @see #shutdown(Socket) 74 | */ 75 | public static void close(Socket socket) { 76 | if (socket == null) return; 77 | 78 | if (!socket.isClosed()) { 79 | shutdown(socket); 80 | try { 81 | socket.close(); 82 | } catch (Throwable e) { 83 | log.debug(e.getMessage(), e); 84 | } 85 | } 86 | } 87 | 88 | /** 89 | * Quietly shutdown the input of a {@linkplain Socket}. Exception are ignored. 90 | * 91 | * @param socket 92 | * the socket to shutdown the input 93 | */ 94 | public static void shutdownInput(Socket socket) { 95 | if (socket == null) return; 96 | try { 97 | if (!socket.isInputShutdown()) socket.shutdownInput(); 98 | } catch (Throwable e) { 99 | log.debug(e.getMessage(), e); 100 | } 101 | } 102 | 103 | /** 104 | * Quietly shutdown the output of a {@linkplain Socket}. Exceptions are ignored. 105 | * 106 | * @param socket 107 | * the socket to shutdown the output 108 | * @see Socket#shutdownOutput() 109 | */ 110 | public static void shutdownOutput(Socket socket) { 111 | if (socket == null) return; 112 | try { 113 | if (!socket.isOutputShutdown()) socket.shutdownOutput(); 114 | } catch (Throwable e) { 115 | log.debug(e.getMessage(), e); 116 | } 117 | } 118 | 119 | /** 120 | * Quietly shutdown both input and output of a {@linkplan Socket}. Exceptions are ignored. 121 | * 122 | * @param socket 123 | * the socket to shutdown 124 | * @see #shutdownInput(Socket) 125 | * @see #shutdownOutput(Socket) 126 | */ 127 | public static void shutdown(Socket socket) { 128 | shutdownInput(socket); 129 | shutdownOutput(socket); 130 | } 131 | 132 | } 133 | -------------------------------------------------------------------------------- /portforward/src/main/java/com/sharneng/io/IOUtils.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2002-2013 the original author or authors. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | package com.sharneng.io; 17 | 18 | import java.io.IOException; 19 | import java.io.InputStream; 20 | import java.io.OutputStream; 21 | 22 | import org.apache.commons.logging.Log; 23 | import org.apache.commons.logging.LogFactory; 24 | 25 | /** 26 | * IO related static utilities methods are defined in this class. 27 | * 28 | * @author Kenneth Xu 29 | * 30 | */ 31 | public final class IOUtils { 32 | private static final Log log = LogFactory.getLog(IOUtils.class); 33 | 34 | private static final int BUF_SIZE = 512; 35 | 36 | private IOUtils() { 37 | } 38 | 39 | /** 40 | * Read everything from in and write to out until reachs the end of the input stream. Both 41 | * in and out are then quitely closed (see {@link #close(InputStream)} ). 42 | * 43 | * @param in 44 | * data copy from 45 | * @param out 46 | * data copy to 47 | * @throws IOException 48 | * IO error occured when coping. 49 | * @see #copyStream(InputStream, OutputStream, boolean) 50 | */ 51 | public static void copyStream(InputStream in, OutputStream out) throws IOException { 52 | copyStream(in, out, true); 53 | } 54 | 55 | /** 56 | * Read everything from in and write to out until reachs the end of the input stream. If 57 | * closeOnFinish is true, it quitely closes both in and out (see 58 | * {@link #close(InputStream)}) before retun. Otherwise, it leaves both of them open. 59 | * 60 | * @param in 61 | * data copy from 62 | * @param out 63 | * data copy to 64 | * @param closeOnFinish 65 | * passing true to close both stream before method return 66 | * @throws IOException 67 | * IO error occured when coping. 68 | * @see #copyStream(InputStream, OutputStream) 69 | */ 70 | public static void copyStream(InputStream in, OutputStream out, boolean closeOnFinish) throws IOException { 71 | byte[] buf = new byte[BUF_SIZE]; 72 | int count; 73 | try { 74 | while ((count = in.read(buf)) != -1) { 75 | out.write(buf, 0, count); 76 | } 77 | } finally { 78 | if (closeOnFinish) close(in); 79 | if (closeOnFinish) close(out); 80 | } 81 | } 82 | 83 | /** 84 | * Quietly close a {@linkplain java.io.InputStream}. Exceptions are discarded. 85 | * 86 | * @param in 87 | * the input stream to close 88 | * @see #close(InputStream, boolean) 89 | */ 90 | public static void close(InputStream in) { 91 | close(in, false); 92 | } 93 | 94 | /** 95 | * Quietly close a {@linkplain java.io.InputStream} without throwing any exception. Exceptions will be logged as 96 | * error if the logError is true. 97 | * 98 | * @param in 99 | * the input stream to close 100 | * @param logError 101 | * true to log exception as error 102 | * @see #close(InputStream) 103 | * @see #close(OutputStream, boolean) 104 | */ 105 | public static void close(InputStream in, boolean logError) { 106 | if (in != null) { 107 | try { 108 | in.close(); 109 | } catch (Throwable t) { 110 | if (logError) log.error(t.getMessage(), t); 111 | else log.debug(t.getMessage(), t); 112 | } 113 | } 114 | } 115 | 116 | /** 117 | * Quietly close a {@linkplain java.io.OutputStream}. Exceptions are discarded. 118 | * 119 | * @param out 120 | * the output stream to close 121 | * @see #close(OutputStream, boolean) 122 | */ 123 | public static void close(OutputStream out) { 124 | close(out, false); 125 | } 126 | 127 | /** 128 | * Quietly close a {@linkplain java.io.OutputStream} without throwing any exception. Exceptions will be logged as 129 | * error if the logError is true. 130 | * 131 | * @param out 132 | * the output stream to close 133 | * @param logError 134 | * true to log exception as error 135 | * @see #close(OutputStream) 136 | * @see #close(InputStream, boolean) 137 | */ 138 | public static void close(OutputStream out, boolean logError) { 139 | if (out != null) { 140 | try { 141 | out.close(); 142 | } catch (Throwable t) { 143 | if (logError) log.error(t.getMessage(), t); 144 | else log.debug(t.getMessage(), t); 145 | } 146 | } 147 | } 148 | } 149 | -------------------------------------------------------------------------------- /portforward/checkstyle-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 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 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | -------------------------------------------------------------------------------- /portforward/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | com.sharneng 6 | portforward 7 | 1.0.1-SNAPSHOT 8 | jar 9 | 10 | 11 | org.sonatype.oss 12 | oss-parent 13 | 7 14 | 15 | 16 | portforward 17 | http://kennethxu.github.io/portforward 18 | 19 | 20 | UTF-8 21 | 3.0.1 22 | 24 | 1.1 25 | 4.8.2 26 | 1.4.12 27 | 28 | A popular Java based TCP/IP port forwarding utility 29 | 30 | 31 | The Apache Software License, Version 2.0 32 | http://www.apache.org/licenses/LICENSE-2.0.txt 33 | repo 34 | 35 | 36 | 37 | scm:git:https://github.com/kennethxu/portforward.git 38 | scm:git:https://github.com/kennethxu/portforward.git 39 | https://github.com/kennethxu/portforward 40 | 41 | 42 | 43 | kennethxu 44 | Kenneth Xu 45 | kennethxu at sharneng dot com 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | org.apache.maven.plugins 54 | maven-release-plugin 55 | 2.2.1 56 | 57 | 58 | default 59 | 60 | perform 61 | 62 | 63 | lookup/pom.xml 64 | 65 | 66 | 67 | 68 | 69 | org.apache.maven.plugins 70 | maven-compiler-plugin 71 | 72 | 1.6 73 | 1.6 74 | 75 | 76 | 77 | org.codehaus.mojo 78 | cobertura-maven-plugin 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | org.apache.maven.plugins 90 | maven-surefire-plugin 91 | 2.12.4 92 | 93 | 94 | false 95 | true 96 | 97 | **/*Test.java 98 | 99 | true 100 | 101 | 102 | 103 | 104 | 105 | 106 | org.codehaus.mojo 107 | cobertura-maven-plugin 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | maven-assembly-plugin 117 | 2.4 118 | 119 | 120 | 121 | com.sharneng.net.portforward.Forwarder 122 | 123 | 124 | 125 | cli 126 | 127 | 128 | 129 | 130 | make-assembly 131 | package 132 | 133 | single 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | org.apache.maven.plugins 145 | maven-checkstyle-plugin 146 | 2.8 147 | 148 | checkstyle-config.xml 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | junit 157 | junit 158 | ${junit.version} 159 | test 160 | 161 | 162 | org.mockito 163 | mockito-all 164 | 1.8.5 165 | test 166 | 167 | 168 | org.powermock 169 | powermock-module-junit4 170 | ${powermock.version} 171 | test 172 | 173 | 174 | org.powermock 175 | powermock-api-mockito 176 | ${powermock.version} 177 | test 178 | 179 | 180 | org.hamcrest 181 | hamcrest-all 182 | ${hamcrest.version} 183 | jar 184 | test 185 | 186 | 187 | com.google.code.findbugs 188 | annotations 189 | 2.0.1 190 | provided 191 | 192 | 193 | org.slf4j 194 | slf4j-nop 195 | 1.7.2 196 | test 197 | 198 | 199 | com.google.code.findbugs 200 | jsr305 201 | 2.0.1 202 | 203 | 204 | commons-logging 205 | commons-logging 206 | 1.1.3 207 | 208 | 209 | 210 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | 2 | Apache License 3 | Version 2.0, January 2004 4 | http://www.apache.org/licenses/ 5 | 6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 7 | 8 | 1. Definitions. 9 | 10 | "License" shall mean the terms and conditions for use, reproduction, 11 | and distribution as defined by Sections 1 through 9 of this document. 12 | 13 | "Licensor" shall mean the copyright owner or entity authorized by 14 | the copyright owner that is granting the License. 15 | 16 | "Legal Entity" shall mean the union of the acting entity and all 17 | other entities that control, are controlled by, or are under common 18 | control with that entity. For the purposes of this definition, 19 | "control" means (i) the power, direct or indirect, to cause the 20 | direction or management of such entity, whether by contract or 21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 22 | outstanding shares, or (iii) beneficial ownership of such entity. 23 | 24 | "You" (or "Your") shall mean an individual or Legal Entity 25 | exercising permissions granted by this License. 26 | 27 | "Source" form shall mean the preferred form for making modifications, 28 | including but not limited to software source code, documentation 29 | source, and configuration files. 30 | 31 | "Object" form shall mean any form resulting from mechanical 32 | transformation or translation of a Source form, including but 33 | not limited to compiled object code, generated documentation, 34 | and conversions to other media types. 35 | 36 | "Work" shall mean the work of authorship, whether in Source or 37 | Object form, made available under the License, as indicated by a 38 | copyright notice that is included in or attached to the work 39 | (an example is provided in the Appendix below). 40 | 41 | "Derivative Works" shall mean any work, whether in Source or Object 42 | form, that is based on (or derived from) the Work and for which the 43 | editorial revisions, annotations, elaborations, or other modifications 44 | represent, as a whole, an original work of authorship. For the purposes 45 | of this License, Derivative Works shall not include works that remain 46 | separable from, or merely link (or bind by name) to the interfaces of, 47 | the Work and Derivative Works thereof. 48 | 49 | "Contribution" shall mean any work of authorship, including 50 | the original version of the Work and any modifications or additions 51 | to that Work or Derivative Works thereof, that is intentionally 52 | submitted to Licensor for inclusion in the Work by the copyright owner 53 | or by an individual or Legal Entity authorized to submit on behalf of 54 | the copyright owner. For the purposes of this definition, "submitted" 55 | means any form of electronic, verbal, or written communication sent 56 | to the Licensor or its representatives, including but not limited to 57 | communication on electronic mailing lists, source code control systems, 58 | and issue tracking systems that are managed by, or on behalf of, the 59 | Licensor for the purpose of discussing and improving the Work, but 60 | excluding communication that is conspicuously marked or otherwise 61 | designated in writing by the copyright owner as "Not a Contribution." 62 | 63 | "Contributor" shall mean Licensor and any individual or Legal Entity 64 | on behalf of whom a Contribution has been received by Licensor and 65 | subsequently incorporated within the Work. 66 | 67 | 2. Grant of Copyright License. Subject to the terms and conditions of 68 | this License, each Contributor hereby grants to You a perpetual, 69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 70 | copyright license to reproduce, prepare Derivative Works of, 71 | publicly display, publicly perform, sublicense, and distribute the 72 | Work and such Derivative Works in Source or Object form. 73 | 74 | 3. Grant of Patent License. Subject to the terms and conditions of 75 | this License, each Contributor hereby grants to You a perpetual, 76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 77 | (except as stated in this section) patent license to make, have made, 78 | use, offer to sell, sell, import, and otherwise transfer the Work, 79 | where such license applies only to those patent claims licensable 80 | by such Contributor that are necessarily infringed by their 81 | Contribution(s) alone or by combination of their Contribution(s) 82 | with the Work to which such Contribution(s) was submitted. If You 83 | institute patent litigation against any entity (including a 84 | cross-claim or counterclaim in a lawsuit) alleging that the Work 85 | or a Contribution incorporated within the Work constitutes direct 86 | or contributory patent infringement, then any patent licenses 87 | granted to You under this License for that Work shall terminate 88 | as of the date such litigation is filed. 89 | 90 | 4. Redistribution. You may reproduce and distribute copies of the 91 | Work or Derivative Works thereof in any medium, with or without 92 | modifications, and in Source or Object form, provided that You 93 | meet the following conditions: 94 | 95 | (a) You must give any other recipients of the Work or 96 | Derivative Works a copy of this License; and 97 | 98 | (b) You must cause any modified files to carry prominent notices 99 | stating that You changed the files; and 100 | 101 | (c) You must retain, in the Source form of any Derivative Works 102 | that You distribute, all copyright, patent, trademark, and 103 | attribution notices from the Source form of the Work, 104 | excluding those notices that do not pertain to any part of 105 | the Derivative Works; and 106 | 107 | (d) If the Work includes a "NOTICE" text file as part of its 108 | distribution, then any Derivative Works that You distribute must 109 | include a readable copy of the attribution notices contained 110 | within such NOTICE file, excluding those notices that do not 111 | pertain to any part of the Derivative Works, in at least one 112 | of the following places: within a NOTICE text file distributed 113 | as part of the Derivative Works; within the Source form or 114 | documentation, if provided along with the Derivative Works; or, 115 | within a display generated by the Derivative Works, if and 116 | wherever such third-party notices normally appear. The contents 117 | of the NOTICE file are for informational purposes only and 118 | do not modify the License. You may add Your own attribution 119 | notices within Derivative Works that You distribute, alongside 120 | or as an addendum to the NOTICE text from the Work, provided 121 | that such additional attribution notices cannot be construed 122 | as modifying the License. 123 | 124 | You may add Your own copyright statement to Your modifications and 125 | may provide additional or different license terms and conditions 126 | for use, reproduction, or distribution of Your modifications, or 127 | for any such Derivative Works as a whole, provided Your use, 128 | reproduction, and distribution of the Work otherwise complies with 129 | the conditions stated in this License. 130 | 131 | 5. Submission of Contributions. Unless You explicitly state otherwise, 132 | any Contribution intentionally submitted for inclusion in the Work 133 | by You to the Licensor shall be under the terms and conditions of 134 | this License, without any additional terms or conditions. 135 | Notwithstanding the above, nothing herein shall supersede or modify 136 | the terms of any separate license agreement you may have executed 137 | with Licensor regarding such Contributions. 138 | 139 | 6. Trademarks. This License does not grant permission to use the trade 140 | names, trademarks, service marks, or product names of the Licensor, 141 | except as required for reasonable and customary use in describing the 142 | origin of the Work and reproducing the content of the NOTICE file. 143 | 144 | 7. Disclaimer of Warranty. Unless required by applicable law or 145 | agreed to in writing, Licensor provides the Work (and each 146 | Contributor provides its Contributions) on an "AS IS" BASIS, 147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 148 | implied, including, without limitation, any warranties or conditions 149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 150 | PARTICULAR PURPOSE. You are solely responsible for determining the 151 | appropriateness of using or redistributing the Work and assume any 152 | risks associated with Your exercise of permissions under this License. 153 | 154 | 8. Limitation of Liability. In no event and under no legal theory, 155 | whether in tort (including negligence), contract, or otherwise, 156 | unless required by applicable law (such as deliberate and grossly 157 | negligent acts) or agreed to in writing, shall any Contributor be 158 | liable to You for damages, including any direct, indirect, special, 159 | incidental, or consequential damages of any character arising as a 160 | result of this License or out of the use or inability to use the 161 | Work (including but not limited to damages for loss of goodwill, 162 | work stoppage, computer failure or malfunction, or any and all 163 | other commercial damages or losses), even if such Contributor 164 | has been advised of the possibility of such damages. 165 | 166 | 9. Accepting Warranty or Additional Liability. While redistributing 167 | the Work or Derivative Works thereof, You may choose to offer, 168 | and charge a fee for, acceptance of support, warranty, indemnity, 169 | or other liability obligations and/or rights consistent with this 170 | License. However, in accepting such obligations, You may act only 171 | on Your own behalf and on Your sole responsibility, not on behalf 172 | of any other Contributor, and only if You agree to indemnify, 173 | defend, and hold each Contributor harmless for any liability 174 | incurred by, or claims asserted against, such Contributor by reason 175 | of your accepting any such warranty or additional liability. 176 | 177 | END OF TERMS AND CONDITIONS 178 | 179 | APPENDIX: How to apply the Apache License to your work. 180 | 181 | To apply the Apache License to your work, attach the following 182 | boilerplate notice, with the fields enclosed by brackets "[]" 183 | replaced with your own identifying information. (Don't include 184 | the brackets!) The text should be enclosed in the appropriate 185 | comment syntax for the file format. We also recommend that a 186 | file or class name and description of purpose be included on the 187 | same "printed page" as the copyright notice for easier 188 | identification within third-party archives. 189 | 190 | Copyright [yyyy] [name of copyright owner] 191 | 192 | Licensed under the Apache License, Version 2.0 (the "License"); 193 | you may not use this file except in compliance with the License. 194 | You may obtain a copy of the License at 195 | 196 | http://www.apache.org/licenses/LICENSE-2.0 197 | 198 | Unless required by applicable law or agreed to in writing, software 199 | distributed under the License is distributed on an "AS IS" BASIS, 200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 201 | See the License for the specific language governing permissions and 202 | limitations under the License. 203 | --------------------------------------------------------------------------------