Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
FinClip
ligase
提交
db428174
ligase
项目概览
FinClip
/
ligase
通知
37
Star
8
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
ligase
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
db428174
编写于
4月 20, 2017
作者:
K
Kegsay
提交者:
GitHub
4月 20, 2017
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
tool: Add client-api-proxy (#73)
上级
fad7e954
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
122 addition
and
0 deletion
+122
-0
src/github.com/matrix-org/dendrite/cmd/client-api-proxy/main.go
...thub.com/matrix-org/dendrite/cmd/client-api-proxy/main.go
+122
-0
未找到文件。
src/github.com/matrix-org/dendrite/cmd/client-api-proxy/main.go
0 → 100644
浏览文件 @
db428174
package
main
import
(
"flag"
"fmt"
log
"github.com/Sirupsen/logrus"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"
"time"
)
const
usage
=
`Usage: %s
Create a single endpoint URL which clients can be pointed at.
The client-server API in Dendrite is split across multiple processes
which listen on multiple ports. You cannot point a Matrix client at
any of those ports, as there will be unimplemented functionality.
In addition, all client-server API processes start with the additional
path prefix '/api', which Matrix clients will be unaware of.
This tool will proxy requests for all client-server URLs and forward
them to their respective process. It will also add the '/api' path
prefix to incoming requests.
THIS TOOL IS FOR TESTING AND NOT INTENDED FOR PRODUCTION USE.
Arguments:
`
var
(
syncServerURL
=
flag
.
String
(
"sync-server-url"
,
""
,
"The base URL of the listening 'dendrite-sync-server' process. E.g. 'http://localhost:4200'"
)
clientAPIURL
=
flag
.
String
(
"client-api-url"
,
""
,
"The base URL of the listening 'dendrite-client-api' process. E.g. 'http://localhost:4321'"
)
bindAddress
=
flag
.
String
(
"bind-address"
,
":8008"
,
"The listening port for the proxy."
)
)
func
makeProxy
(
targetURL
string
)
(
*
httputil
.
ReverseProxy
,
error
)
{
if
!
strings
.
HasSuffix
(
targetURL
,
"/"
)
{
targetURL
+=
"/"
}
// Check that we can parse the URL.
_
,
err
:=
url
.
Parse
(
targetURL
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
httputil
.
ReverseProxy
{
Director
:
func
(
req
*
http
.
Request
)
{
// URL.Path() removes the % escaping from the path.
// The % encoding will be added back when the url is encoded
// when the request is forwarded.
// This means that we will lose any unessecary escaping from the URL.
// Pratically this means that any distinction between '%2F' and '/'
// in the URL will be lost by the time it reaches the target.
path
:=
req
.
URL
.
Path
path
=
"api"
+
path
log
.
WithFields
(
log
.
Fields
{
"path"
:
path
,
"url"
:
targetURL
,
"method"
:
req
.
Method
,
})
.
Print
(
"proxying request"
)
newURL
,
err
:=
url
.
Parse
(
targetURL
+
path
)
if
err
!=
nil
{
// We already checked that we can parse the URL
// So this shouldn't ever get hit.
panic
(
err
)
}
// Copy the query parameters from the request.
newURL
.
RawQuery
=
req
.
URL
.
RawQuery
req
.
URL
=
newURL
},
},
nil
}
func
main
()
{
flag
.
Usage
=
func
()
{
fmt
.
Fprintf
(
os
.
Stderr
,
usage
,
os
.
Args
[
0
])
flag
.
PrintDefaults
()
}
flag
.
Parse
()
if
*
syncServerURL
==
""
{
flag
.
Usage
()
fmt
.
Fprintln
(
os
.
Stderr
,
"no --sync-server-url specified."
)
os
.
Exit
(
1
)
}
if
*
clientAPIURL
==
""
{
flag
.
Usage
()
fmt
.
Fprintln
(
os
.
Stderr
,
"no --client-api-url specified."
)
os
.
Exit
(
1
)
}
syncProxy
,
err
:=
makeProxy
(
*
syncServerURL
)
if
err
!=
nil
{
panic
(
err
)
}
clientProxy
,
err
:=
makeProxy
(
*
clientAPIURL
)
if
err
!=
nil
{
panic
(
err
)
}
http
.
Handle
(
"/_matrix/client/r0/sync"
,
syncProxy
)
http
.
Handle
(
"/"
,
clientProxy
)
srv
:=
&
http
.
Server
{
Addr
:
*
bindAddress
,
ReadTimeout
:
1
*
time
.
Minute
,
// how long we wait for the client to send the entire request (after connection accept)
WriteTimeout
:
5
*
time
.
Minute
,
// how long the proxy has to write the full response
}
fmt
.
Println
(
"Proxying requests to:"
)
fmt
.
Println
(
" /_matrix/client/r0/sync => "
,
*
syncServerURL
+
"/api/_matrix/client/r0/sync"
)
fmt
.
Println
(
" /* => "
,
*
clientAPIURL
+
"/api/*"
)
fmt
.
Println
(
"Listening on "
,
*
bindAddress
)
srv
.
ListenAndServe
()
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录