绑定:请求的地址在其上下文中无效

问题描述

我在golang上开发了一个机器人。它是使用ubuntu 20.01 OS在vds上启动的,效果很好,但是当我开始调试代码时这是一个问题。因此,我决定将PC用作VDS:我打开了 8443 端口等。但是,当 main.go 启动时,我会收到错误消息:>

listen tcp [ip]:8443: bind: The requested address is not valid in its context.

我的代码

package main

import (
    "./configuration"
    "./get_data"
    "encoding/json"
    "fmt"
    tgBotApi "github.com/go-telegram-bot-api/telegram-bot-api"
    "io/IoUtil"
    "net/http"
    "strings"
    "time"
)

var (
    NewBot,BotErr = tgBotApi.NewBotAPI(configuration.BOT_TOKEN)
)
func setWebhook(bot *tgBotApi.BotAPI) {
    webHookInfo := tgBotApi.NewWebhookWithCert(fmt.Sprintf("https://%s:%s/%s",configuration.BOT_HOST,configuration.BOT_PORT,configuration.BOT_TOKEN),configuration.CERT_FILE)
    _,err := bot.SetWebhook(webHookInfo)
    if err != nil {
        fmt.Println(err)
    }
}
func main () {
    fmt.Println("OK",time.Now().Unix(),time.Now(),time.Now().Weekday())
    setWebhook(NewBot)

    message := func (w http.ResponseWriter,r *http.Request) {
        text,_ := IoUtil.ReadAll(r.Body)
        var bottext = get_data.BotMessage{}
        _ = json.Unmarshal(text,&bottext)
        chatGroup := int64(bottext.Message.Chat.Id)
        botCommand := strings.Split(bottext.Message.Text,"@")[0]

        /*markup := tgBotApi.InlineKeyboardMarkup{
            InlineKeyboard: [][]tgBotApi.InlineKeyboardButton{
                []tgBotApi.InlineKeyboardButton{
                    tgBotApi.InlineKeyboardButton{Text: "start"},},}*/
        //reply := tgBotApi.NewEditMessageReplyMarkup(chatGroup,messageId,markup)
        var msg tgBotApi.MessageConfig
        switch botCommand {
            case "/start":
                msg = tgBotApi.NewMessage(chatGroup,helloCommandMsg())
                msg.ReplyMarkup = tgBotApi.NewReplyKeyboard(
                    []tgBotApi.KeyboardButton{catalogBtn.Btn},[]tgBotApi.KeyboardButton{myProfileBtn.Btn,supportBtn.Btn},[]tgBotApi.KeyboardButton{getLuckyBtn.Btn,rulesBtn.Btn},[]tgBotApi.KeyboardButton{addFundsBtn.Btn},)
                break
            case getLuckyBtn.Btn.Text:
                getLuckyBtn.Action(bottext,NewBot)
                break
            case myProfileBtn.Btn.Text:
                myProfileBtn.Action(bottext,NewBot)
                break
            case addFundsBtn.Btn.Text:
                addFundsBtn.Action(bottext,NewBot)
                break
            default:
                msg = tgBotApi.NewMessage(chatGroup,unkNownCommandMsg())
        }
        NewBot.Send(msg)
    }
    http.HandleFunc("/",message)
    errListenTLS := http.ListenAndServeTLS(fmt.Sprintf("%s:%s",configuration.BOT_PORT),configuration.CERT_FILE,configuration.CERT_KEY,nil)
    if errListenTLS != nil {
        fmt.Println(errListenTLS)
    }
}

configuration / main_config.go:

package configuration

const (
    TELEGRAM_URL = "https://api.telegram.org/bot"
    BOT_TOKEN = MYTOKEN
    BOT_HOST = "[ip]"
    BOT_PORT = "8443"
)

configuration / cert_config.go:

package configuration

const (
    CERT_FILE = "C:\\Users\\USER\\Desktop\\Golocal\\BOTlocal\\certificates\\mybot.pem"
    CERT_KEY = "C:\\Users\\USER\\Desktop\\Golocal\\BOTlocal\\certificates\\mybot.key"
)

我已经按照以下主题生成了证书:What is easy way to create and use a Self-Signed Certification for a Telegram Webhook?

此外,我尝试设置0.0.0.0而不是我的公共IP,然后出现此错误

Bad Request: bad webhook: IP address 0.0.0.0 is reserved

解决方法

错误$(document).ready(function() { $('.sidebar-link').click(function(e) { e.preventDefault(); var $this = $(this); document.querySelectorAll('.sidebar-link').forEach(el=> { el.classList.remove('show') }); e.classList.add('show'); $this.parent().parent().find('sidebar-submenu-item .sidebar-submenu').removeClass('show'); $this.parent().parent().find('sidebar-submenu-item .sidebar-submenu').slideUp(350); $this.next().toggleClass('show'); $this.next().slideToggle(350); }); 表示该地址不属于您计算机上的网络接口。可能有一个带有实际公共地址的路由器/负载平衡器/等,它会将流量转发到您的计算机。

您需要拆分使用的地址:

  • 您的本地地址(请参见bind: The requested address is not valid in its context.)或ifconfig(作为所有接口的地址)传递给0.0.0.0
  • http.ListenAndServeTLS中传递的公用地址作为回调地址