@@ -39,14 +39,13 @@ func init() {
3939}
4040
4141type TTYd struct {
42- mux * http.ServeMux
42+ mux * http.ServeMux
43+ upgrader websocket.Upgrader
4344}
4445
4546type CmdGenerator func () * exec.Cmd
4647
47- var upgrader = websocket.Upgrader {} // use default options
48-
49- func ws (w http.ResponseWriter , r * http.Request , gen CmdGenerator , connCounter * int32 , max int32 ) {
48+ func ws (upgrader websocket.Upgrader , w http.ResponseWriter , r * http.Request , gen CmdGenerator , connCounter * int32 , max int32 ) {
5049 c , err := upgrader .Upgrade (w , r , nil )
5150 if err != nil {
5251 Logger .Print ("upgrade:" , err )
@@ -83,11 +82,13 @@ type Config struct {
8382 OtherFSList []fs.FS // Other fs that need to be served as static files.
8483 Gen CmdGenerator // A generator that creates the actual command.
8584 MaxConn int32 // Maximum number of connections. Unlimited if <= 0.
85+ CheckOrigin func (r * http.Request ) bool
8686}
8787
8888func NewTTYd (conf Config ) * TTYd {
8989 ttyd := & TTYd {
90- mux : http .NewServeMux (),
90+ mux : http .NewServeMux (),
91+ upgrader : websocket.Upgrader {CheckOrigin : conf .CheckOrigin },
9192 }
9293 if conf .MaxConn < 0 {
9394 conf .MaxConn = 0
@@ -104,7 +105,7 @@ func NewTTYd(conf Config) *TTYd {
104105 Pair : map [string ]string {},
105106 }, "/index.html" ))
106107 ttyd .mux .HandleFunc ("/ws" , func (w http.ResponseWriter , r * http.Request ) {
107- ws (w , r , conf .Gen , & connCounter , conf .MaxConn )
108+ ws (ttyd . upgrader , w , r , conf .Gen , & connCounter , conf .MaxConn )
108109 })
109110 ttyd .mux .HandleFunc ("/themes.json" , func (w http.ResponseWriter , r * http.Request ) {
110111 themes := ThemeList (serveFS )
0 commit comments