├── .github ├── CODEOWNERS └── workflows │ ├── docker.yml │ └── deb.yml ├── .gitignore ├── spec ├── spec_helper.cr └── amqp-websocket-proxy_spec.cr ├── src ├── websocket-tcp-relay │ ├── version.cr │ ├── prefix_handler.cr │ └── websocket_relay.cr └── websocket-tcp-relay.cr ├── shard.lock ├── shard.yml ├── Dockerfile.deb ├── Dockerfile ├── README.md └── LICENSE /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @cloudamqp/crystal 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /docs/ 2 | /lib/ 3 | /bin/ 4 | /.shards/ 5 | *.dwarf 6 | -------------------------------------------------------------------------------- /spec/spec_helper.cr: -------------------------------------------------------------------------------- 1 | require "spec" 2 | require "../src/websocket-tcp-relay/websocket_relay" 3 | -------------------------------------------------------------------------------- /src/websocket-tcp-relay/version.cr: -------------------------------------------------------------------------------- 1 | module WebSocketTCPRelay 2 | VERSION = {{ `shards version`.stringify }} 3 | end 4 | -------------------------------------------------------------------------------- /shard.lock: -------------------------------------------------------------------------------- 1 | version: 2.0 2 | shards: 3 | amq-protocol: 4 | git: https://github.com/cloudamqp/amq-protocol.cr.git 5 | version: 1.1.14 6 | 7 | -------------------------------------------------------------------------------- /spec/amqp-websocket-proxy_spec.cr: -------------------------------------------------------------------------------- 1 | require "./spec_helper" 2 | 3 | describe WebSocketTCPRelay::WebSocketRelay do 4 | # TODO: Write tests 5 | 6 | it "works" do 7 | false.should eq(true) 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /shard.yml: -------------------------------------------------------------------------------- 1 | name: websocket-tcp-relay 2 | version: 1.0.8 3 | 4 | authors: 5 | - Carl Hörberg 6 | 7 | targets: 8 | websocket-tcp-relay: 9 | main: src/websocket-tcp-relay.cr 10 | 11 | dependencies: 12 | amq-protocol: 13 | github: cloudamqp/amq-protocol.cr 14 | 15 | crystal: 1.0.0 16 | 17 | license: Apache-2.0 18 | -------------------------------------------------------------------------------- /Dockerfile.deb: -------------------------------------------------------------------------------- 1 | ARG build_image 2 | FROM $build_image AS build-stage 3 | 4 | WORKDIR /build 5 | 6 | # Copy all files 7 | COPY shard.yml shard.lock README.md LICENSE ./ 8 | COPY src src 9 | COPY build build 10 | 11 | # Build deb package 12 | ARG pkg_version 13 | RUN build/deb $pkg_version 14 | 15 | # Copy the deb package to a scratch image, that then can be exported 16 | FROM scratch AS export-stage 17 | COPY --from=build-stage /build/builds . 18 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM 84codes/crystal:1.13.3-alpine-latest AS builder 2 | WORKDIR /tmp 3 | COPY shard.yml shard.lock ./ 4 | RUN shards install --production 5 | COPY src/ src/ 6 | RUN shards build --production --release --no-debug 7 | 8 | FROM alpine:latest 9 | RUN apk add --no-cache libssl3 pcre2 libevent libgcc 10 | COPY --from=builder /tmp/bin/* /usr/bin/ 11 | USER 2:2 12 | EXPOSE 15670 13 | ENTRYPOINT ["/usr/bin/websocket-tcp-relay", "--bind=0.0.0.0"] 14 | -------------------------------------------------------------------------------- /src/websocket-tcp-relay/prefix_handler.cr: -------------------------------------------------------------------------------- 1 | require "http/server/handler" 2 | 3 | class PrefixHandler 4 | include HTTP::Handler 5 | 6 | def initialize(prefix : String) 7 | @prefix = "/#{prefix.strip('/')}/" 8 | end 9 | 10 | def call(context) 11 | if @prefix == "//" 12 | call_next(context) 13 | elsif context.request.path.starts_with? @prefix 14 | context.request.path = context.request.path[@prefix.bytesize - 1..] 15 | call_next(context) 16 | else 17 | context.response.respond_with_status(404) 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /.github/workflows/docker.yml: -------------------------------------------------------------------------------- 1 | name: Docker images 2 | 3 | on: 4 | push: 5 | branches: 6 | - 'main' 7 | tags: 8 | - 'v*' 9 | 10 | jobs: 11 | docker: 12 | runs-on: ubuntu-latest 13 | steps: 14 | - name: Checkout 15 | uses: actions/checkout@v4 16 | 17 | - name: Docker meta 18 | id: meta 19 | uses: docker/metadata-action@v5 20 | with: 21 | images: cloudamqp/websocket-tcp-relay 22 | flavor: | 23 | latest=true 24 | tags: | 25 | type=raw,value=latest,enable={{is_default_branch}} 26 | type=semver,pattern={{version}} 27 | 28 | - name: Set up QEMU 29 | uses: docker/setup-qemu-action@v3 30 | 31 | - name: Set up Docker Buildx 32 | uses: docker/setup-buildx-action@v3 33 | 34 | - name: Login to DockerHub 35 | if: github.event_name != 'pull_request' 36 | uses: docker/login-action@v3 37 | with: 38 | username: ${{ secrets.DOCKERHUB_USERNAME }} 39 | password: ${{ secrets.DOCKERHUB_TOKEN }} 40 | 41 | - name: Build and push 42 | uses: docker/build-push-action@v5 43 | with: 44 | platforms: linux/amd64,linux/arm64 45 | push: ${{ github.event_name != 'pull_request' }} 46 | tags: ${{ steps.meta.outputs.tags }} 47 | labels: ${{ steps.meta.outputs.labels }} 48 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WebSocket TCP Relay 2 | 3 | WebSocket server that relay traffic to any TCP server. It also serves static files from `--webroot` directory. 4 | 5 | ## Installation 6 | 7 | Debian/Ubuntu: 8 | 9 | ```bash 10 | wget -qO- https://packagecloud.io/cloudamqp/websocket-tcp-relay/gpgkey | sudo gpg --dearmor -o /etc/apt/keyrings/cloudamqp-websocket-tcp-relay.gpg 11 | echo "deb [signed-by=/etc/apt/keyrings/cloudamqp-websocket-tcp-relay.gpg] https://packagecloud.io/cloudamqp/websocket-tcp-relay/ubuntu/ $(lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/websocket-tcp-relay.list 12 | 13 | sudo apt update 14 | sudo apt install websocket-tcp-relay 15 | ``` 16 | 17 | Docker/Podman: 18 | 19 | Docker images are published to [Docker Hub](https://hub.docker.com/r/cloudamqp/websocket-tcp-relay). Fetch and run the latest version with: 20 | 21 | `docker run --rm -it -p 15670:15670 cloudamqp/websocket-tcp-relay --upstream tcp://container:5672` 22 | 23 | ## Usage 24 | 25 | ``` 26 | Usage: websocket-tcp-relay [arguments] 27 | -u URI, --upstream=URI Upstream (eg. tcp://localhost:5672 or tls://127.0.0.1:5671) 28 | -b HOST, --bind=HOST Address to bind to (default localhost) 29 | -p PORT, --port=PORT Address to bind to (default 15670) 30 | --tls-cert=PATH TLS certificate + chain (default ./certs/fullchain.pem) 31 | --tls-key=PATH TLS certificate key (default ./certs/privkey.pem) 32 | -P, --proxy-protocol If the upstream expects the PROXY protocol (default false) 33 | -w PATH, --webroot=PATH Directory from which to serve static content (default ./webroot) 34 | -c PATH, --config=PATH Config file 35 | -v, --version Display version number 36 | -h, --help Show this help 37 | ``` 38 | 39 | Example config file: 40 | 41 | ```ini 42 | [main] 43 | upstream = tcp://127.0.0.1:5672 44 | bind = 127.0.0.1 45 | port = 15670 46 | proxy-protocol = false 47 | webroot = /var/lib/wwwroot 48 | tls-cert = /etc/ssl/certs/fullchain.pem 49 | tls-key = /etc/ssl/private/privkey.pem 50 | ``` 51 | -------------------------------------------------------------------------------- /.github/workflows/deb.yml: -------------------------------------------------------------------------------- 1 | name: Debian packages 2 | on: 3 | workflow_dispatch: 4 | push: 5 | branches: 6 | - 'main' 7 | tags: 8 | - 'v*.*.*' 9 | 10 | jobs: 11 | build_deb: 12 | name: Build 13 | strategy: 14 | fail-fast: false 15 | matrix: 16 | arch: [amd64, arm64] 17 | os: [ubuntu-20.04, ubuntu-22.04, ubuntu-24.04, debian-bullseye, debian-bookworm] 18 | runs-on: ubuntu-latest 19 | steps: 20 | - name: Checkout 21 | uses: actions/checkout@v4 22 | with: 23 | fetch-depth: 0 24 | 25 | - name: Get version 26 | run: echo "PKG_VERSION=$(git describe --tags | cut -c2-)" >> $GITHUB_ENV 27 | 28 | - name: Set up QEMU 29 | uses: docker/setup-qemu-action@v3 30 | 31 | - name: Set up Docker Buildx 32 | uses: docker/setup-buildx-action@v3 33 | 34 | - name: Build image and output result to builds 35 | uses: docker/build-push-action@v5 36 | with: 37 | context: . 38 | file: Dockerfile.deb 39 | platforms: linux/${{ matrix.arch }} 40 | build-args: | 41 | build_image=84codes/crystal:1.13.3-${{ matrix.os }} 42 | pkg_version=${{ env.PKG_VERSION }} 43 | outputs: builds 44 | 45 | - name: Upload deb to packagecloud 46 | if: startsWith(github.ref, 'refs/tags/v') 47 | run: | 48 | set -eux 49 | curl -fsSO -u "${{ secrets.packagecloud_token }}:" https://packagecloud.io/api/v1/distributions.json 50 | PKG_FILE=$(find builds -name "*.deb" | head -1) 51 | ID=$(echo $PKG_FILE | cut -d/ -f2) 52 | VERSION_CODENAME=$(echo $PKG_FILE | cut -d/ -f3) 53 | DIST_ID=$(jq ".deb[] | select(.index_name == \"${ID}\").versions[] | select(.index_name == \"${VERSION_CODENAME}\").id" distributions.json) 54 | curl -fsS -u "${{ secrets.packagecloud_token }}:" -XPOST \ 55 | -F "package[distro_version_id]=${DIST_ID}" \ 56 | -F "package[package_file]=@${PKG_FILE}" \ 57 | https://packagecloud.io/api/v1/repos/${{ github.repository }}/packages.json 58 | -------------------------------------------------------------------------------- /src/websocket-tcp-relay/websocket_relay.cr: -------------------------------------------------------------------------------- 1 | require "http/server" 2 | require "openssl" 3 | require "amq-protocol" 4 | 5 | module WebSocketTCPRelay 6 | class WebSocketRelay 7 | def self.new(host : String, port : Int32, tls : Bool, proxy_protocol : Bool) 8 | tls_ctx = OpenSSL::SSL::Context::Client.new if tls 9 | ::HTTP::WebSocketHandler.new do |ws, ctx| 10 | req = ctx.request 11 | local_addr = req.local_address.as(Socket::IPAddress) 12 | remote_addr = remote_address(req.headers) || req.remote_address.as(Socket::IPAddress) 13 | puts "#{remote_addr} connected" 14 | tcp_socket = TCPSocket.new(host, port, dns_timeout: 5, connect_timeout: 15) 15 | tcp_socket.tcp_nodelay = true 16 | tcp_socket.keepalive = true 17 | tcp_socket.tcp_keepalive_idle = 60 18 | tcp_socket.tcp_keepalive_interval = 10 19 | tcp_socket.tcp_keepalive_count = 3 20 | socket = 21 | if ctx = tls_ctx 22 | OpenSSL::SSL::Socket::Client.new(tcp_socket, ctx, hostname: host).tap do |c| 23 | c.sync_close = true 24 | end 25 | else 26 | tcp_socket 27 | end 28 | if proxy_protocol 29 | if remote_addr.@family.inet6? || local_addr.@family.inet6? 30 | socket << "PROXY TCP6 " 31 | socket << "::ffff:" if remote_addr.@family.inet? 32 | socket << remote_addr.address << " " 33 | socket << "::ffff:" if local_addr.@family.inet? 34 | socket << local_addr.address << " " 35 | else 36 | socket << "PROXY TCP4 " << remote_addr.address << " " << local_addr.address << " " 37 | end 38 | socket << remote_addr.port << " " << local_addr.port << "\r\n" 39 | socket.flush 40 | end 41 | socket.as?(TCPSocket).try &.sync = true 42 | socket.as?(OpenSSL::SSL::Socket::Client).try &.sync = true 43 | 44 | ws.on_close do |_code, _message| 45 | socket.close 46 | end 47 | 48 | amqp_protocol = Channel(Bool).new 49 | first_bytes = true 50 | ws.on_binary do |bytes| 51 | if first_bytes 52 | first_bytes = false 53 | if bytes == AMQ::Protocol::PROTOCOL_START_0_9_1 54 | amqp_protocol.send true 55 | else 56 | amqp_protocol.send false 57 | end 58 | end 59 | socket.write(bytes) 60 | end 61 | spawn(name: "WS #{remote_addr}") do 62 | begin 63 | if amqp_protocol.receive 64 | mem = IO::Memory.new(4096) 65 | loop do 66 | frame = AMQ::Protocol::Frame.from_io(socket) 67 | frame.to_io(mem, IO::ByteFormat::NetworkEndian) 68 | ws.send(mem.to_slice) 69 | mem.clear 70 | end 71 | else 72 | buffer = Bytes.new(4096) 73 | count = 0 74 | while (count = socket.read(buffer)) > 0 75 | ws.send(buffer[0, count]) 76 | end 77 | end 78 | puts "#{remote_addr} disconnected by server" 79 | rescue ex 80 | puts "#{remote_addr} disconnected: #{ex.inspect}" 81 | ensure 82 | ws.close rescue nil 83 | socket.close rescue nil 84 | end 85 | end 86 | puts "#{remote_addr} connected to upstream" 87 | rescue ex 88 | puts "#{remote_addr} disconnected: #{ex.inspect}" 89 | socket.try(&.close) rescue nil 90 | ws.close rescue nil 91 | end 92 | end 93 | 94 | def self.remote_address(headers) 95 | if fwd = headers["Forwarded"]? 96 | if match = /^[Ff]or=(([\d.]+)|\[([\d:.A-Fa-f]+)\])(:(\d{1,5}))?[;,]?/.match(fwd) 97 | ip = match[2] || match[3] 98 | port = match[5].try(&.to_i) || 0 99 | Socket::IPAddress.new(ip, port) 100 | else 101 | puts "Invalid Forwarded header: '#{fwd}'" 102 | end 103 | elsif xfwd = headers["X-Forwarded-For"]? 104 | ip = (idx = xfwd.index(',')) ? xfwd[0, idx] : xfwd 105 | port = 0 106 | if xport = headers["X-Forwarded-Port"]? 107 | port = xport.to_i 108 | end 109 | Socket::IPAddress.new(ip, port) 110 | elsif ip = headers["X-Real-IP"]? 111 | Socket::IPAddress.new(ip, 0) 112 | end 113 | end 114 | end 115 | end 116 | -------------------------------------------------------------------------------- /src/websocket-tcp-relay.cr: -------------------------------------------------------------------------------- 1 | require "option_parser" 2 | require "ini" 3 | require "./websocket-tcp-relay/*" 4 | 5 | module WebSocketTCPRelay 6 | def self.run 7 | bind_addr = "localhost" 8 | bind_port = ENV.fetch("PORT", "").to_i? || 15670 9 | webroot = "./webroot" 10 | tls_cert_path = "" 11 | tls_key_path = "" 12 | upstream_uri = nil 13 | proxy_protocol = false 14 | prefix = "/" 15 | 16 | OptionParser.parse do |parser| 17 | parser.banner = "Usage: #{File.basename PROGRAM_NAME} [arguments]" 18 | parser.on("-u URI", "--upstream=URI", "Upstream (eg. tcp://localhost:5672 or tls://127.0.0.1:5671)") do |v| 19 | upstream_uri = URI.parse(v) 20 | end 21 | parser.on("-b HOST", "--bind=HOST", "Address to bind to (default #{bind_addr})") do |v| 22 | bind_addr = v 23 | end 24 | parser.on("-p PORT", "--port=PORT", "Address to bind to (default #{bind_port})") do |v| 25 | bind_port = v.to_i? || abort "Invalid port number" 26 | end 27 | parser.on("--tls-cert=PATH", "TLS certificate + chain (default #{tls_cert_path})") do |v| 28 | tls_cert_path = v 29 | end 30 | parser.on("--tls-key=PATH", "TLS certificate key (default #{tls_key_path})") do |v| 31 | tls_key_path = v 32 | end 33 | parser.on("-P", "--proxy-protocol", "If the upstream expects the PROXY protocol (default #{proxy_protocol})") do 34 | proxy_protocol = true 35 | end 36 | parser.on("-w PATH", "--webroot=PATH", "Directory from which to serve static content (default #{webroot})") do |v| 37 | webroot = v 38 | end 39 | parser.on("--prefix=PATH", "Path prefix (default #{prefix})") do |v| 40 | prefix = v 41 | end 42 | parser.on("-c PATH", "--config=PATH", "Config file") do |v| 43 | config = File.open(v) { |f| INI.parse(f) } 44 | config.each do |name, section| 45 | case name 46 | when "main" 47 | section.each do |key, value| 48 | case key 49 | when "upstream" then upstream_uri = URI.parse(value) 50 | when "bind" then bind_addr = value 51 | when "port" then bind_port = value.to_i 52 | when "tls-cert" then tls_cert_path = value 53 | when "tls-key" then tls_key_path = value 54 | when "proxy-protocol" then proxy_protocol = /^(true|1|on)$/.matches?(value) 55 | when "webroot" then webroot = value 56 | when "prefix" then prefix = value 57 | else abort "Unrecognized config: #{name}/#{key}" 58 | end 59 | end 60 | else abort "Unrecognized config section: #{name}" 61 | end 62 | end 63 | rescue File::NotFoundError 64 | abort "Config file not found" 65 | end 66 | parser.on("-v", "--version", "Display version number") do 67 | puts VERSION 68 | exit 69 | end 70 | parser.on("-h", "--help", "Show this help") do 71 | puts parser 72 | exit 73 | end 74 | parser.invalid_option do |flag| 75 | STDERR.puts "ERROR: #{flag} is not a valid option." 76 | STDERR.puts parser 77 | exit 1 78 | end 79 | end 80 | 81 | if u = upstream_uri 82 | MIME.register(".mjs", "text/javascript;charset=utf-8") # ecmascript modules 83 | 84 | server = HTTP::Server.new([ 85 | WebSocketRelay.new(u.host || "127.0.0.1", u.port || 5672, u.scheme == "tls", proxy_protocol), 86 | PrefixHandler.new(prefix), 87 | HTTP::StaticFileHandler.new(webroot, fallthrough: false, directory_listing: false), 88 | ]) 89 | 90 | address = nil 91 | protocol = "http" 92 | if !tls_cert_path.empty? || !tls_key_path.empty? 93 | File.exists?(tls_cert_path) || abort "Certificate not found" 94 | File.exists?(tls_key_path) || abort "Private key not found" 95 | context = OpenSSL::SSL::Context::Server.new 96 | context.certificate_chain = tls_cert_path 97 | context.private_key = tls_key_path 98 | address = server.bind_tls bind_addr, bind_port, context 99 | protocol = "https" 100 | else 101 | address = server.bind_tcp bind_addr, bind_port 102 | end 103 | puts "Listening: #{protocol}://#{address}" 104 | puts "Upstream: #{u}" 105 | puts "PROXY protocol: #{proxy_protocol ? "enabled" : "disabled"}" 106 | puts "Web root: #{Dir.exists?(webroot) ? File.expand_path webroot : "Not found"}" 107 | puts "Path: #{prefix}" 108 | Signal::INT.trap { server.close } 109 | Signal::TERM.trap { server.close } 110 | server.listen 111 | else 112 | abort "An upstream is required, must specify the --upstream flag" 113 | end 114 | end 115 | end 116 | 117 | WebSocketTCPRelay.run 118 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 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 | --------------------------------------------------------------------------------