登录
首页 >  Golang >  Go问答

连接错误:地址在此上下文中无效

来源:stackoverflow

时间:2024-03-05 19:48:28 266浏览 收藏

怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面golang学习网就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《连接错误:地址在此上下文中无效》,涉及到,有需要的可以收藏一下

问题内容

我在 golang 上开发了一个机器人。在使用 ubuntu 20.01 操作系统的 vds 上启动它,效果很好,但当我开始调试代码时,这是一个问题。因此,我决定使用我的电脑作为 vds:我打开了 8443 端口等。但是当 ma​​in.go 启动时,我收到错误: p>

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_host, configuration.bot_port), configuration.cert_file, configuration.cert_key, nil)
    if errlistentls != nil {
        fmt.println(errlistentls)
    }
}

配置/main_config.go:

package configuration

const (
    telegram_url = "https://api.telegram.org/bot"
    bot_token = mytoken
    bot_host = "[ip]"
    bot_port = "8443"
)

配置/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"
)

我已经生成了本主题中的证书:为 telegram webhook 创建和使用自签名证书的简单方法是什么?

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

Bad Request: bad webhook: IP address 0.0.0.0 is reserved

解决方案


错误 bind:请求的地址在其上下文中无效。 表示该地址不属于您计算机上的网络接口。可能有一个路由器/负载均衡器/等等...具有实际的公共地址,并且它将流量转发到您的计算机。

您需要拆分您使用的地址:

  • 您的本地地址(请参阅 ifconfig)或所有接口的 0.0.0.0 作为传递给 http.ListenAndServeTLS 的地址
  • 公共地址作为NewWebhookWithCert中传入的回调地址

理论要掌握,实操不能落!以上关于《连接错误:地址在此上下文中无效》的详细介绍,大家都掌握了吧!如果想要继续提升自己的能力,那么就来关注golang学习网公众号吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>