VirtualBox

Ignore:
Timestamp:
Apr 29, 2022 8:55:44 AM (3 years ago)
Author:
vboxsync
Message:

Main/VBoxManage/Update check: More code + docs for proxy settings handling. bugref:7983

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-server/UpdateAgentImpl.cpp

    r94737 r94756  
    4343#include "VirtualBoxImpl.h"
    4444#include "VBoxEvents.h"
     45#include "SystemPropertiesImpl.h"
    4546#include "ThreadTask.h"
    4647#include "VirtualBoxImpl.h"
     
    265266}
    266267
     268/**
     269 * Returns the proxy mode as a string.
     270 *
     271 * @returns Proxy mode as string.
     272 * @param   enmMode             Proxy mode to return as string.
     273 */
     274/* static */
     275const char *UpdateAgentBase::i_proxyModeToStr(ProxyMode_T enmMode)
     276{
     277    switch (enmMode)
     278    {
     279        case ProxyMode_System:  return "System";
     280        case ProxyMode_Manual:  return "Manual";
     281        case ProxyMode_NoProxy: return "None";
     282        default:                break;
     283    }
     284
     285    AssertFailed();
     286    return "<Invalid>";
     287}
     288
    267289
    268290/*********************************************************************************************************************************
     
    296318    HRESULT hr = unconst(m_EventSource).createObject();
    297319    if (SUCCEEDED(hr))
     320    {
    298321        hr = m_EventSource->init();
     322        if (SUCCEEDED(hr))
     323            mData.m_fUseOwnProxy = false;
     324    }
    299325
    300326    return hr;
     
    522548    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
    523549
    524     *aMode = m->enmProxyMode;
    525 
    526     return S_OK;
     550    return i_getProxyMode(aMode);
    527551}
    528552
     
    532556
    533557    m->enmProxyMode = aMode;
     558    mData.m_fUseOwnProxy = true;
    534559
    535560    return i_commitSettings(alock);
     
    540565    AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
    541566
    542     aAddress = m->strProxyUrl;
    543 
    544     return S_OK;
     567    return i_getProxyURL(aAddress);
    545568}
    546569
     
    550573
    551574    m->strProxyUrl = aAddress;
     575    mData.m_fUseOwnProxy = true;
    552576
    553577    return i_commitSettings(alock);
     
    677701    data = *m;
    678702
     703    /* Cancel out eventually set proxy settings if those were not explicitly set.
     704     * This way the ISystemProperties proxy settings will be used then. */
     705    if (!mData.m_fUseOwnProxy)
     706    {
     707        data.strProxyUrl  = "";
     708        data.enmProxyMode = ProxyMode_System;
     709    }
     710
    679711    return S_OK;
    680712}
     
    731763    AutoWriteLock vboxLock(m_VirtualBox COMMA_LOCKVAL_SRC_POS);
    732764    return m_VirtualBox->i_saveSettings();
     765}
     766
     767/**
     768 * Returns the proxy mode to use.
     769 *
     770 * @returns HRESULT
     771 * @param   aMode               Where to return the proxy mode.
     772 */
     773HRESULT UpdateAgent::i_getProxyMode(ProxyMode_T *aMode)
     774{
     775    HRESULT hrc;
     776
     777    if (!mData.m_fUseOwnProxy) /* If not explicitly set, use the ISystemProperties proxy settings. */
     778    {
     779        ComPtr<ISystemProperties> pSystemProperties;
     780        hrc = m_VirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
     781        if (SUCCEEDED(hrc))
     782            hrc = pSystemProperties->COMGETTER(ProxyMode)(aMode);
     783    }
     784    else
     785    {
     786        *aMode = m->enmProxyMode;
     787        hrc = S_OK;
     788    }
     789
     790    return hrc;
     791}
     792
     793/**
     794 * Returns the proxy URL to use.
     795 *
     796 * @returns HRESULT
     797 * @param   aUrl                Where to return the proxy URL to use.
     798 */
     799HRESULT UpdateAgent::i_getProxyURL(com::Utf8Str &aUrl)
     800{
     801    HRESULT hrc;
     802
     803    if (!mData.m_fUseOwnProxy) /* If not explicitly set, use the ISystemProperties proxy settings. */
     804    {
     805        ComPtr<ISystemProperties> pSystemProperties;
     806        hrc = m_VirtualBox->COMGETTER(SystemProperties)(pSystemProperties.asOutParam());
     807        if (SUCCEEDED(hrc))
     808        {
     809            com::Bstr bstrVal;
     810            hrc = pSystemProperties->COMGETTER(ProxyURL)(bstrVal.asOutParam());
     811            if (SUCCEEDED(hrc))
     812                aUrl = bstrVal;
     813        }
     814    }
     815    else
     816    {
     817        aUrl = m->strProxyUrl;
     818        hrc = S_OK;
     819    }
     820
     821    return hrc;
     822}
     823
     824/**
     825 * Configures a HTTP client's proxy.
     826 *
     827 * @returns HRESULT
     828 * @param   hHttp               HTTP client to configure proxy for.
     829 */
     830HRESULT UpdateAgent::i_configureProxy(RTHTTP hHttp)
     831{
     832    HRESULT rc;
     833
     834    ProxyMode_T enmProxyMode;
     835    rc = i_getProxyMode(&enmProxyMode);
     836    ComAssertComRCRetRC(rc);
     837    Utf8Str strProxyUrl;
     838    rc = i_getProxyURL(strProxyUrl);
     839    ComAssertComRCRetRC(rc);
     840
     841    if (enmProxyMode == ProxyMode_Manual)
     842    {
     843        int vrc = RTHttpSetProxyByUrl(hHttp, strProxyUrl.c_str());
     844        if (RT_FAILURE(vrc))
     845            return i_reportError(vrc, tr("RTHttpSetProxyByUrl() failed: %Rrc"), vrc);
     846    }
     847    else if (enmProxyMode == ProxyMode_System)
     848    {
     849        int vrc = RTHttpUseSystemProxySettings(hHttp);
     850        if (RT_FAILURE(vrc))
     851            return i_reportError(vrc, tr("RTHttpUseSystemProxySettings() failed: %Rrc"), vrc);
     852    }
     853    else
     854        Assert(enmProxyMode == ProxyMode_NoProxy);
     855
     856    LogRel2(("Update agent (%s): Using proxy mode = '%s', URL = '%s'\n",
     857             mData.m_strName.c_str(), UpdateAgentBase::i_proxyModeToStr(enmProxyMode), strProxyUrl.c_str()));
     858
     859    return S_OK;
    733860}
    734861
     
    9781105HRESULT HostUpdateAgent::i_checkForUpdateInner(RTHTTP hHttp, Utf8Str const &strUrl, Utf8Str const &strUserAgent)
    9791106{
     1107    /*
     1108     * Configure the proxy (if any).
     1109     */
     1110    HRESULT rc = i_configureProxy(hHttp);
     1111    if (FAILED(rc))
     1112        return rc;
     1113
    9801114    /** @todo Are there any other headers needed to be added first via RTHttpSetHeaders()? */
    9811115    int vrc = RTHttpAddHeader(hHttp, "User-Agent", strUserAgent.c_str(), strUserAgent.length(), RTHTTPADDHDR_F_BACK);
    9821116    if (RT_FAILURE(vrc))
    9831117        return i_reportError(vrc, tr("RTHttpAddHeader() failed: %Rrc (user agent)"), vrc);
    984 
    985     /*
    986      * Configure proxying.
    987      */
    988     if (m->enmProxyMode == ProxyMode_Manual)
    989     {
    990         vrc = RTHttpSetProxyByUrl(hHttp, m->strProxyUrl.c_str());
    991         if (RT_FAILURE(vrc))
    992             return i_reportError(vrc, tr("RTHttpSetProxyByUrl() failed: %Rrc"), vrc);
    993     }
    994     else if (m->enmProxyMode == ProxyMode_System)
    995     {
    996         vrc = RTHttpUseSystemProxySettings(hHttp);
    997         if (RT_FAILURE(vrc))
    998             return i_reportError(vrc, tr("RTHttpUseSystemProxySettings() failed: %Rrc"), vrc);
    999     }
    1000     else
    1001         Assert(m->enmProxyMode == ProxyMode_NoProxy);
    10021118
    10031119    /*
     
    10351151    size_t const cchWord1 = (size_t)(pchResponse - pchWord1);
    10361152
    1037     HRESULT rc;
    1038 
    10391153    /* Decode the two word: */
    10401154    static char const s_szUpToDate[] = "UPTODATE";
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette