From 0310a2c7c0684253759015a42702d0d3f62096c0 Mon Sep 17 00:00:00 2001
From: Shelikhoo <xiaokangwang@outlook.com>
Date: Fri, 14 Oct 2022 17:16:11 +0100
Subject: [PATCH] Add WebTunnel Integration

This is a squashed commit that includes:
Replace cgo Condition in Go Config
Include WebTunnel README in Browser
Include WebTunnel into Tor Expert Bundle
Add WebTunnel to default torrc
Add WebTunnel project

Their original form is available at:
https://gitlab.torproject.org/shelikhoo/tor-browser-build-2/-/commits/dev-webtunnel-rebased-backup

See also: https://gitlab.torproject.org/tpo/applications/tor-browser-build/-/merge_requests/595
---
 .../PTConfigs/linux/torrc-defaults-appendix   |  3 ++
 .../PTConfigs/mac/torrc-defaults-appendix     |  3 ++
 .../PTConfigs/windows/torrc-defaults-appendix |  3 ++
 projects/browser/build                        |  6 +++-
 projects/go/config                            |  2 +-
 projects/tor-expert-bundle/build              |  1 +
 projects/tor-expert-bundle/config             |  4 ++-
 projects/webtunnel/build                      | 34 +++++++++++++++++++
 projects/webtunnel/config                     | 17 ++++++++++
 9 files changed, 70 insertions(+), 3 deletions(-)
 create mode 100644 projects/webtunnel/build
 create mode 100644 projects/webtunnel/config

diff --git a/projects/browser/Bundle-Data/PTConfigs/linux/torrc-defaults-appendix b/projects/browser/Bundle-Data/PTConfigs/linux/torrc-defaults-appendix
index c15471bce..34aaedc46 100644
--- a/projects/browser/Bundle-Data/PTConfigs/linux/torrc-defaults-appendix
+++ b/projects/browser/Bundle-Data/PTConfigs/linux/torrc-defaults-appendix
@@ -3,3 +3,6 @@ ClientTransportPlugin meek_lite,obfs2,obfs3,obfs4,scramblesuit exec ./TorBrowser
 
 ## snowflake configuration
 ClientTransportPlugin snowflake exec ./TorBrowser/Tor/PluggableTransports/snowflake-client
+
+## webtunnel configuration
+ClientTransportPlugin webtunnel exec ./TorBrowser/Tor/PluggableTransports/webtunnel-client
diff --git a/projects/browser/Bundle-Data/PTConfigs/mac/torrc-defaults-appendix b/projects/browser/Bundle-Data/PTConfigs/mac/torrc-defaults-appendix
index 020b071ad..9e17f053c 100644
--- a/projects/browser/Bundle-Data/PTConfigs/mac/torrc-defaults-appendix
+++ b/projects/browser/Bundle-Data/PTConfigs/mac/torrc-defaults-appendix
@@ -3,3 +3,6 @@ ClientTransportPlugin meek_lite,obfs2,obfs3,obfs4,scramblesuit exec PluggableTra
 
 ## snowflake configuration
 ClientTransportPlugin snowflake exec PluggableTransports/snowflake-client
+
+## webtunnel configuration
+ClientTransportPlugin webtunnel exec PluggableTransports/webtunnel-client
diff --git a/projects/browser/Bundle-Data/PTConfigs/windows/torrc-defaults-appendix b/projects/browser/Bundle-Data/PTConfigs/windows/torrc-defaults-appendix
index 00166e08c..7c593a5ef 100644
--- a/projects/browser/Bundle-Data/PTConfigs/windows/torrc-defaults-appendix
+++ b/projects/browser/Bundle-Data/PTConfigs/windows/torrc-defaults-appendix
@@ -3,3 +3,6 @@ ClientTransportPlugin meek_lite,obfs2,obfs3,obfs4,scramblesuit exec TorBrowser\T
 
 ## snowflake configuration
 ClientTransportPlugin snowflake exec TorBrowser\Tor\PluggableTransports\snowflake-client.exe
+
+## webtunnel configuration
+ClientTransportPlugin webtunnel exec TorBrowser\Tor\PluggableTransports\webtunnel-client.exe
diff --git a/projects/browser/build b/projects/browser/build
index 57544b461..cb42b5fb6 100644
--- a/projects/browser/build
+++ b/projects/browser/build
@@ -88,9 +88,13 @@ mv [% c('input_files_by_name/noscript') %] "$TBDIR/$EXTSPATH/{73a6fe31-595d-460b
     rm -f aarch64/tor/pluggable_transports/bridges_list.*.txt
   [% END -%]
 
-  # Snowflake's README is the only documentation shipped in tor-expert-bundle
+  # Snowflake's README is a documentation shipped in tor-expert-bundle
   mkdir -p "$TBDIR/$DOCSPATH/snowflake" [% IF c("var/macos_universal") %]"$TBDIR_AARCH64/$DOCSPATH/snowflake"[% END %]
   mv_tbdir tor/pluggable_transports/README.SNOWFLAKE.md "$DOCSPATH/snowflake/README.md"
+  
+  # WebTunnel's README is a documentation shipped in tor-expert-bundle
+  mkdir -p "$TBDIR/$DOCSPATH/webtunnel" [% IF c("var/macos_universal") %]"$TBDIR_AARCH64/$DOCSPATH/webtunnel"[% END %]
+  mv_tbdir tor/pluggable_transports/README.WEBTUNNEL.md "$DOCSPATH/webtunnel/README.md"
 
   # Move the PTs to where TB expects them
   mkdir -p "$TBDIR/$TORBINPATH" [% IF c("var/macos_universal") %]"$TBDIR_AARCH64/$TORBINPATH"[% END %]
diff --git a/projects/go/config b/projects/go/config
index 976f5485a..c21c7765f 100644
--- a/projects/go/config
+++ b/projects/go/config
@@ -26,7 +26,7 @@ var:
       export CGO_LDFLAGS_ALLOW="-z|noexecstack"
       export CGO_LDFLAGS="-z noexecstack"
     [% END -%]
-    [% IF c("var/linux-i686") -%]
+    [% IF c("var/cgo") -%]
       export CGO_ENABLED=1
     [% END -%]
     export GOTMPDIR=/var/tmp/build/go-tmp
diff --git a/projects/tor-expert-bundle/build b/projects/tor-expert-bundle/build
index 0a8957c5c..b8bd55587 100644
--- a/projects/tor-expert-bundle/build
+++ b/projects/tor-expert-bundle/build
@@ -11,6 +11,7 @@ mkdir pluggable_transports && cd pluggable_transports
 
 tar -xkf $rootdir/[% c('input_files_by_name/obfs4') %]
 tar -xkf $rootdir/[% c('input_files_by_name/snowflake') %]
+tar -xkf $rootdir/[% c('input_files_by_name/webtunnel') %]
 
 # copy in bridge lines for each pluggable transport
 mv $rootdir/bridges_list.obfs4.txt .
diff --git a/projects/tor-expert-bundle/config b/projects/tor-expert-bundle/config
index 894539efe..e93421da0 100644
--- a/projects/tor-expert-bundle/config
+++ b/projects/tor-expert-bundle/config
@@ -13,6 +13,8 @@ input_files:
     project: obfs4
   - name: snowflake
     project: snowflake
+  - project: webtunnel
+    name: webtunnel
   - filename: bridges_list.obfs4.txt
   - filename: bridges_list.meek-azure.txt
-  - filename: bridges_list.snowflake.txt
+  - filename: bridges_list.snowflake.txt
\ No newline at end of file
diff --git a/projects/webtunnel/build b/projects/webtunnel/build
new file mode 100644
index 000000000..79450554c
--- /dev/null
+++ b/projects/webtunnel/build
@@ -0,0 +1,34 @@
+#!/bin/bash
+[% c("var/set_default_env") -%]
+[% pc('go', 'var/setup', { go_tarfile => c('input_files_by_name/go') }) %]
+distdir=/var/tmp/dist/[% project %]
+mkdir -p $distdir
+
+[% IF c("var/android") -%]
+  [% pc(c('var/compiler'), 'var/setup', { compiler_tarfile => c('input_files_by_name/' _ c('var/compiler')) }) %]
+  # We need to explicitly set CGO_ENABLED with Go 1.13.x as the Android build
+  # breaks otherwise.
+  export CGO_ENABLED=1
+[% END -%]
+
+tar -C /var/tmp/dist -xf [% c('input_files_by_name/goptlib') %]
+
+mkdir -p /var/tmp/build
+tar -C /var/tmp/build -xf [% project %]-[% c('version') %].tar.gz
+cd /var/tmp/build/[% project %]-[% c('version') %]
+
+mkdir -p "$GOPATH/src/gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/webtunnel.git"
+ln -sf "$PWD" "$GOPATH/src/gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/webtunnel"
+
+cd main/client
+go build -ldflags '-s'
+cp -a client[% IF c("var/windows") %].exe[% END %] $distdir/webtunnel-client[% IF c("var/windows") %].exe[% END %]
+
+cd /var/tmp/build/[% project %]-[% c('version') %]
+cp -a README.md $distdir/README.WEBTUNNEL.md
+
+cd $distdir
+[% c('tar', {
+        tar_src => [ '.' ],
+        tar_args => '-czf ' _ dest_dir _ '/' _ c('filename'),
+    }) %]
diff --git a/projects/webtunnel/config b/projects/webtunnel/config
new file mode 100644
index 000000000..ef26aeffa
--- /dev/null
+++ b/projects/webtunnel/config
@@ -0,0 +1,17 @@
+# vim: filetype=yaml sw=2
+version: '[% c("abbrev") %]'
+git_url: https://gitlab.torproject.org/tpo/anti-censorship/pluggable-transports/webtunnel.git
+git_hash: 2ecf64381bad8a5db45c5871dcad7d12831dca69
+filename: '[% project %]-[% c("version") %]-[% c("var/osname") %]-[% c("var/build_id") %].tar.gz'
+container:
+  use_container: 1
+
+input_files:
+  - project: container-image
+  - name: go
+    project: go
+  - name: goptlib
+    project: goptlib
+  - name: '[% c("var/compiler") %]'
+    project: '[% c("var/compiler") %]'
+    enable: '[% c("var/android") %]'
-- 
GitLab