Permalink
Browse files

Switch to Heroku, rewrite server in Go

This replaces the Google App Engine Python web proxy with a faster, portable version written in Go. It uses the standard net/http package and can be deployed to Heroku without leaving the web browser.

Because Go expects all code to be kept in a single workspace, the UWP projects are moved to a separate Git repo.
  • Loading branch information...
timothy003 committed May 31, 2018
1 parent 38154fa commit 0346b1f081b1b1ef54efb09fb74c0ec38a235bd5
Showing with 186 additions and 1,698 deletions.
  1. +0 −409 .gitignore
  2. +1 −0 Procfile
  3. +3 −14 README.md
  4. +9 −0 app.json
  5. +0 −17 appengine/app.dev.yaml
  6. +0 −15 appengine/app.yaml
  7. +0 −83 appengine/main.py
  8. 0 {appengine → }/links.html
  9. +166 −0 main.go
  10. +1 −1 {appengine → }/scripts.html
  11. BIN {appengine → }/static/icon-144.png
  12. 0 {appengine → }/static/manifest.json
  13. 0 {appengine → }/static/mobilediscord.css
  14. 0 {appengine → }/static/mobilediscord.js
  15. 0 {appengine → }/static/smoothscroll.js
  16. 0 {appengine → }/static/splash.js
  17. 0 {appengine → }/static/webrtc_winJS_api.js
  18. +6 −0 vendor/vendor.json
  19. +0 −154 windows/MobileDiscord.sln
  20. +0 −116 windows/MobileDiscord/MobileDiscord.jsproj
  21. +0 −1 windows/MobileDiscord/build_info.js
  22. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-16.png
  23. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-20.png
  24. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-24.png
  25. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-256.png
  26. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-30.png
  27. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-32.png
  28. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-36.png
  29. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-40.png
  30. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-48.png
  31. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-60.png
  32. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-64.png
  33. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-72.png
  34. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-80.png
  35. BIN windows/MobileDiscord/images/DiscordAppList.altform-unplated_targetsize-96.png
  36. BIN windows/MobileDiscord/images/DiscordAppList.scale-100.png
  37. BIN windows/MobileDiscord/images/DiscordAppList.scale-125.png
  38. BIN windows/MobileDiscord/images/DiscordAppList.scale-150.png
  39. BIN windows/MobileDiscord/images/DiscordAppList.scale-200.png
  40. BIN windows/MobileDiscord/images/DiscordAppList.scale-400.png
  41. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-16.png
  42. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-20.png
  43. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-24.png
  44. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-256.png
  45. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-30.png
  46. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-32.png
  47. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-36.png
  48. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-40.png
  49. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-48.png
  50. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-60.png
  51. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-64.png
  52. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-72.png
  53. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-80.png
  54. BIN windows/MobileDiscord/images/DiscordAppList.targetsize-96.png
  55. BIN windows/MobileDiscord/images/DiscordPackageLogo.scale-100.png
  56. BIN windows/MobileDiscord/images/DiscordPackageLogo.scale-125.png
  57. BIN windows/MobileDiscord/images/DiscordPackageLogo.scale-150.png
  58. BIN windows/MobileDiscord/images/DiscordPackageLogo.scale-200.png
  59. BIN windows/MobileDiscord/images/DiscordPackageLogo.scale-400.png
  60. +0 −39 windows/MobileDiscord/package.appxmanifest
  61. +0 −116 windows/MobileDiscordCanary/MobileDiscordCanary.jsproj
  62. +0 −1 windows/MobileDiscordCanary/build_info.js
  63. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-16.png
  64. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-20.png
  65. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-24.png
  66. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-256.png
  67. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-30.png
  68. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-32.png
  69. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-36.png
  70. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-40.png
  71. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-48.png
  72. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-60.png
  73. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-64.png
  74. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-72.png
  75. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-80.png
  76. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.altform-unplated_targetsize-96.png
  77. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.scale-100.png
  78. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.scale-125.png
  79. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.scale-150.png
  80. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.scale-200.png
  81. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.scale-400.png
  82. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-16.png
  83. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-20.png
  84. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-24.png
  85. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-256.png
  86. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-30.png
  87. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-32.png
  88. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-36.png
  89. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-40.png
  90. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-48.png
  91. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-60.png
  92. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-64.png
  93. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-72.png
  94. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-80.png
  95. BIN windows/MobileDiscordCanary/images/DiscordCanaryAppList.targetsize-96.png
  96. BIN windows/MobileDiscordCanary/images/DiscordCanaryPackageLogo.scale-100.png
  97. BIN windows/MobileDiscordCanary/images/DiscordCanaryPackageLogo.scale-125.png
  98. BIN windows/MobileDiscordCanary/images/DiscordCanaryPackageLogo.scale-150.png
  99. BIN windows/MobileDiscordCanary/images/DiscordCanaryPackageLogo.scale-200.png
  100. BIN windows/MobileDiscordCanary/images/DiscordCanaryPackageLogo.scale-400.png
  101. +0 −39 windows/MobileDiscordCanary/package.appxmanifest
  102. +0 −113 windows/MobileDiscordDev/MobileDiscordDev.jsproj
  103. +0 −1 windows/MobileDiscordDev/build_info.js
  104. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-16.png
  105. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-20.png
  106. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-24.png
  107. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-256.png
  108. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-30.png
  109. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-32.png
  110. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-36.png
  111. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-40.png
  112. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-48.png
  113. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-60.png
  114. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-64.png
  115. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-72.png
  116. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-80.png
  117. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.altform-unplated_targetsize-96.png
  118. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.scale-100.png
  119. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.scale-125.png
  120. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.scale-150.png
  121. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.scale-200.png
  122. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.scale-400.png
  123. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-16.png
  124. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-20.png
  125. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-24.png
  126. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-256.png
  127. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-30.png
  128. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-32.png
  129. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-36.png
  130. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-40.png
  131. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-48.png
  132. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-60.png
  133. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-64.png
  134. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-72.png
  135. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-80.png
  136. BIN windows/MobileDiscordDev/images/DiscordCanaryAppList.targetsize-96.png
  137. BIN windows/MobileDiscordDev/images/DiscordCanaryPackageLogo.scale-100.png
  138. BIN windows/MobileDiscordDev/images/DiscordCanaryPackageLogo.scale-125.png
  139. BIN windows/MobileDiscordDev/images/DiscordCanaryPackageLogo.scale-150.png
  140. BIN windows/MobileDiscordDev/images/DiscordCanaryPackageLogo.scale-200.png
  141. BIN windows/MobileDiscordDev/images/DiscordCanaryPackageLogo.scale-400.png
  142. +0 −39 windows/MobileDiscordDev/package.appxmanifest
  143. +0 −116 windows/MobileDiscordPTB/MobileDiscordPTB.jsproj
  144. +0 −1 windows/MobileDiscordPTB/build_info.js
  145. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-16.png
  146. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-20.png
  147. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-24.png
  148. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-256.png
  149. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-30.png
  150. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-32.png
  151. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-36.png
  152. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-40.png
  153. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-48.png
  154. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-60.png
  155. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-64.png
  156. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-72.png
  157. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-80.png
  158. BIN windows/MobileDiscordPTB/images/DiscordAppList.altform-unplated_targetsize-96.png
  159. BIN windows/MobileDiscordPTB/images/DiscordAppList.scale-100.png
  160. BIN windows/MobileDiscordPTB/images/DiscordAppList.scale-125.png
  161. BIN windows/MobileDiscordPTB/images/DiscordAppList.scale-150.png
  162. BIN windows/MobileDiscordPTB/images/DiscordAppList.scale-200.png
  163. BIN windows/MobileDiscordPTB/images/DiscordAppList.scale-400.png
  164. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-16.png
  165. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-20.png
  166. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-24.png
  167. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-256.png
  168. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-30.png
  169. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-32.png
  170. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-36.png
  171. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-40.png
  172. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-48.png
  173. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-60.png
  174. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-64.png
  175. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-72.png
  176. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-80.png
  177. BIN windows/MobileDiscordPTB/images/DiscordAppList.targetsize-96.png
  178. BIN windows/MobileDiscordPTB/images/DiscordPackageLogo.scale-100.png
  179. BIN windows/MobileDiscordPTB/images/DiscordPackageLogo.scale-125.png
  180. BIN windows/MobileDiscordPTB/images/DiscordPackageLogo.scale-150.png
  181. BIN windows/MobileDiscordPTB/images/DiscordPackageLogo.scale-200.png
  182. BIN windows/MobileDiscordPTB/images/DiscordPackageLogo.scale-400.png
  183. +0 −39 windows/MobileDiscordPTB/package.appxmanifest
  184. +0 −43 windows/MobileDiscordShared/MobileDiscordShared.projitems
  185. +0 −13 windows/MobileDiscordShared/MobileDiscordShared.shproj
  186. +0 −18 windows/MobileDiscordShared/default.css
  187. BIN windows/MobileDiscordShared/fonts/whitney-book.woff
  188. BIN windows/MobileDiscordShared/fonts/whitney-medium.woff
  189. BIN windows/MobileDiscordShared/images/DiscordLargeTile.scale-100.png
  190. BIN windows/MobileDiscordShared/images/DiscordLargeTile.scale-125.png
  191. BIN windows/MobileDiscordShared/images/DiscordLargeTile.scale-150.png
  192. BIN windows/MobileDiscordShared/images/DiscordLargeTile.scale-200.png
  193. BIN windows/MobileDiscordShared/images/DiscordLargeTile.scale-400.png
  194. BIN windows/MobileDiscordShared/images/DiscordMedTile.scale-100.png
  195. BIN windows/MobileDiscordShared/images/DiscordMedTile.scale-125.png
  196. BIN windows/MobileDiscordShared/images/DiscordMedTile.scale-150.png
  197. BIN windows/MobileDiscordShared/images/DiscordMedTile.scale-200.png
  198. BIN windows/MobileDiscordShared/images/DiscordMedTile.scale-400.png
  199. BIN windows/MobileDiscordShared/images/DiscordSmallTile.scale-100.png
  200. BIN windows/MobileDiscordShared/images/DiscordSmallTile.scale-125.png
  201. BIN windows/MobileDiscordShared/images/DiscordSmallTile.scale-150.png
  202. BIN windows/MobileDiscordShared/images/DiscordSmallTile.scale-200.png
  203. BIN windows/MobileDiscordShared/images/DiscordSmallTile.scale-400.png
  204. BIN windows/MobileDiscordShared/images/DiscordSplashScreen.scale-100.png
  205. BIN windows/MobileDiscordShared/images/DiscordSplashScreen.scale-125.png
  206. BIN windows/MobileDiscordShared/images/DiscordSplashScreen.scale-150.png
  207. BIN windows/MobileDiscordShared/images/DiscordSplashScreen.scale-200.png
  208. BIN windows/MobileDiscordShared/images/DiscordSplashScreen.scale-400.png
  209. BIN windows/MobileDiscordShared/images/DiscordWideTile.scale-100.png
  210. BIN windows/MobileDiscordShared/images/DiscordWideTile.scale-125.png
  211. BIN windows/MobileDiscordShared/images/DiscordWideTile.scale-150.png
  212. BIN windows/MobileDiscordShared/images/DiscordWideTile.scale-200.png
  213. BIN windows/MobileDiscordShared/images/DiscordWideTile.scale-400.png
  214. +0 −14 windows/MobileDiscordShared/index.html
  215. +0 −83 windows/MobileDiscordShared/main.js
  216. +0 −109 windows/MobileDiscordShared/msapp-error.css
  217. +0 −26 windows/MobileDiscordShared/msapp-error.html
  218. +0 −78 windows/MobileDiscordShared/msapp-error.js

This file was deleted.

Oops, something went wrong.
@@ -0,0 +1 @@
web: mobilediscord
@@ -4,19 +4,8 @@ MobileDiscord is a custom third-party mod for Discord (https://discordapp.com/).

Neither the code nor its author are affiliated with Discord.

## Dependencies

~~Flask~~ (is now supplied by Google Cloud by default)

## Hosting

1. Create a Google Cloud account at https://cloud.google.com.
2. Create a new project in the web console, name it whatever you want. Write down the project ID (it appears below the project name field in the new project prompt).
3. Download Google Cloud SDK.
4. Run `gcloud auth login`. Your browser will open an authentication page. Login and give permission to SDK.
5. Run `gcloud config set project $PROJECT_ID`.
6. Clone this repository and navigate to `$REPO_ROOT/appengine`.
7. Run `gcloud app regions list`. You'll get a list of available app regions.
8. Run `gcloud app create --region $REGION`, where $REGION is one of the regions from the above list.
9. Run `gcloud app deploy`.
10. Congratulations! MobileDiscord is now available at https://**$PROJECT_ID**.appspot.com/channels/@me
You can host your own copy of the web app on Heroku:

[![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy)
@@ -0,0 +1,9 @@
{
"name": "MobileDiscord",
"description": "Mobile-friendly Discord web app",
"keywords": [
"discord"
],
"website": "https://github.com/timothy003/mobilediscord",
"repository": "https://github.com/timothy003/mobilediscord"
}

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.

This file was deleted.

Oops, something went wrong.
File renamed without changes.
166 main.go
@@ -0,0 +1,166 @@
package main

import (
"bytes"
"context"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"os"
"os/signal"
"strings"
"syscall"
)

var (
links []byte
scripts []byte
)

func main() {
port := os.Getenv("PORT")
if port == "" {
log.Fatal("$PORT must be set")
}

var err error
if links, err = ioutil.ReadFile("links.html"); err != nil {
log.Fatal("failed to read links.html: ", err)
}
if scripts, err = ioutil.ReadFile("scripts.html"); err != nil {
log.Fatal("failed to read scripts.html: ", err)
}

http.Handle("/", &httputil.ReverseProxy{Director: director, ModifyResponse: modifyResponse})
http.Handle("/md/", http.NotFoundHandler())
http.Handle("/assets/md/", http.StripPrefix("/assets/md", addHeaders(http.FileServer(http.Dir("static")))))

srv := &http.Server{Addr: ":" + port}

shutdown := make(chan os.Signal, 1)
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
idleConnsClosed := make(chan struct{})
go func() {
<-shutdown

// We received an interrupt/termination signal, shut down.
if err := srv.Shutdown(context.Background()); err != nil {
// Error from closing listeners, or context timeout:
log.Printf("HTTP server Shutdown: %v", err)
}
close(idleConnsClosed)
}()

if err := srv.ListenAndServe(); err != http.ErrServerClosed {
// Error starting or closing listener:
log.Printf("HTTP server ListenAndServe: %v", err)
}

<-idleConnsClosed
}

func director(req *http.Request) {
req.URL.Scheme = "https"
if strings.HasPrefix(req.Host, "canary") {
req.URL.Host = "canary.discordapp.com"
} else if strings.HasPrefix(req.Host, "ptb") {
req.URL.Host = "ptb.discordapp.com"
} else {
req.URL.Host = "discordapp.com"
}
req.Host = req.URL.Host

if !strings.HasPrefix(req.URL.Path, "/assets/") {
// read uncompressed response
delete(req.Header, "Accept-Encoding")
}

// remove Cloudflare headers (Cloudflare rejects requests with Cf-Connecting-Ip)
for k := range req.Header {
if strings.HasPrefix(k, "Cf-") {
delete(req.Header, k)
}
}

if _, ok := req.Header["User-Agent"]; !ok {
// explicitly disable User-Agent so it's not set to default value
req.Header.Set("User-Agent", "")
}
}

func modifyResponse(res *http.Response) error {
if res.StatusCode >= 500 {
return nil
}

// remove __cfduid cookie to let Cloudflare cache
delete(res.Header, "Set-Cookie")

// hide from search engines
res.Header.Set("X-Robots-Tag", "noindex, nofollow, noarchive, nocache, noimageindex, noodp")

if strings.HasPrefix(res.Request.URL.Path, "/assets/") {
// static assets never expire
switch res.StatusCode {
case http.StatusOK,
http.StatusNonAuthoritativeInfo,
http.StatusPartialContent,
http.StatusNotModified:
if cc := res.Header.Get("Cache-Control"); !strings.Contains(cc, "no-cache") &&
!strings.Contains(cc, "no-store") &&
!strings.Contains(cc, "max-age") &&
res.Header.Get("Expires") == "" &&
res.Header.Get("Last-Modified") != "" {
res.Header.Add("Cache-Control", "max-age=31536000")
}
}
return nil
}

// prevent caching HTML (assets might not load while offline)
if cc := res.Header.Get("Cache-Control"); !strings.Contains(cc, "no-cache") &&
!strings.Contains(cc, "no-store") &&
!strings.Contains(cc, "max-age") {
res.Header.Add("Cache-Control", "max-age=0")
}

if !strings.HasPrefix(res.Header.Get("Content-Type"), "text/html") {
return nil
}

// inject links and scripts
s, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
res.Body.Close()
if i1 := bytes.Index(s, []byte("</head>")); i1 == -1 {
log.Print("modifyResponse: missing </head> tag")
} else if i2 := bytes.Index(s[i1:], []byte("<script ")); i2 == -1 {
log.Print("modifyResponse: missing <script> tag")
} else {
i2 += i1
res.Body = ioutil.NopCloser(io.MultiReader(
bytes.NewReader(s[:i1]),
bytes.NewReader(links),
bytes.NewReader(s[i1:i2]),
strings.NewReader("<script>window.MD_ORIGIN = 'https://"+res.Request.URL.Host+"'</script>\n"),
bytes.NewReader(scripts),
bytes.NewReader(s[i2:]),
))
res.Header.Del("Content-Length")
res.Header.Del("Etag")
return nil
}
res.Body = ioutil.NopCloser(bytes.NewReader(s))
return nil
}

func addHeaders(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "public, max-age=600, stale-if-error=1200")
h.ServeHTTP(w, r)
})
}
@@ -1,4 +1,4 @@
<script src="/assets/md/splash.js"></script>
<script src="/assets/md/splash.js"></script>
<script src="/assets/md/webrtc_winJS_api.js"></script>
<script src="/assets/md/smoothscroll.js"></script>
<script src="/assets/md/mobilediscord.js"></script>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,6 @@
{
"comment": "",
"ignore": "test",
"package": [],
"rootPath": "github.com/timothy003/mobilediscord"
}
Oops, something went wrong.

0 comments on commit 0346b1f

Please sign in to comment.