dtnpf35197 2013-04-23 14:33
浏览 56
已采纳

视频流中的导航,golang中的反向代理

I'm testing a reverse proxy in go. Mainly for playing videos though underlying nginx and streaming videos from other backend servers.

Problem is when navigating through video. For example when playing with vlc through proxy - video starts normally, but stops when trying to navigate. But if i play this video directly from nginx - it works fine.

I expected that on navigation player would create new connection with Range: N- header, but there is no new connections, only when starting video again.

Question:

How does player navigates, when playing video stream? What requests it sends to server? Maybe i'm missing something in connection handling?

This is very basic version for testing, it streams video from local nginx, (local video url - http://localhost/31285611):

package main

import (
    "net/http"
)

func main() {
    (&proxy{}).start()
}

type proxy struct {
    // ...
}

func (p *proxy) start() {
    http.HandleFunc("/play", p.connection)
    http.ListenAndServe("localhost:8040", nil)
}

func (p *proxy) connection(w http.ResponseWriter, r *http.Request) {
    disconnect := make(chan bool, 1)
    go p.send(w, r, disconnect)

    // ...

    <-disconnect
}


func (p *proxy) send(rv http.ResponseWriter, rvq *http.Request, disconnect chan bool) {

    rq, _ := http.NewRequest("GET", "http://localhost/31285611", rvq.Body)
    rq.Header = rvq.Header

    rs, _ := http.DefaultClient.Do(rq)
    for k, v := range rs.Header {
        rv.Header().Set(k, v[0])
    }
    rv.WriteHeader(http.StatusOK)

    buf := make([]byte, 1024)

    // for testing sending only first part.
    for i := 0; i < 100000; i++ {
        n, e := rs.Body.Read(buf[0:])
        if n == 0 || e != nil {
            break
        }
        rv.Write(buf[0:])
    }

    disconnect <- true

}

Update (headers dump):

First player connection:

map[User-Agent:[VLC/2.0.0 LibVLC/2.0.0] Range:[bytes=0-] Connection:[close] Icy-Metadata:[1]]

Response from nginx, when creating connection in go:

map[Server:[nginx/1.3.4] Date:[Tue, 23 Apr 2013 13:29:00 GMT] Content-Type:[application/octet-stream] Content-Length:[8147855699] Last-Modified:[Tue, 21 Aug 2012 20:47:20 GMT] Etag:["5033f3d8-1e5a66953"] Content-Range:[bytes 0-8147855698/8147855699]]
  • 写回答

1条回答 默认 最新

  • 普通网友 2013-05-10 14:13
    关注

    I know it's not really answering your question (and I don't have enough points to comment yet, so sorry for providing this as an answer!) but have you tried using Go's built in http.ReverseProxy (http://golang.org/pkg/net/http/httputil/#ReverseProxy)?

    There seems to be a nice, simple example here https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/1ufEw_IEVM4 which I've very slightly modified below:

    package main
    
    import (
        "log"
        "net/http"
        "net/http/httputil"
        "net/url"
    )
    
    func main() {
        proxy := httputil.NewSingleHostReverseProxy(&url.URL{Scheme: "http", Host: "www.google.com", Path: "/"})
    
        err := http.ListenAndServe(":8080", proxy)
        if err != nil {
            log.Fatal("ListenAndServe: ", err)
        }
    }
    

    See if that does the job.

    Also, in the previously linked Google Groups discussion, there is mention of NginX having issues with chunked encoding. It might be worth checking if this is related.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效